2012-06-15 26 views
8

Ciao Sto cercando di associare una lista <> a una casella combinata.associazione combobox wpf

<ComboBox Margin="131,242,275,33" x:Name="customer" Width="194" Height="25"/> 

public OfferEditPage() 
    { 
     InitializeComponent(); 
     cusmo = new CustomerViewModel(); 
     DataContext = this; 
     Cusco = cusmo.Customer.ToList<Customer>(); 
     customer.ItemsSource = Cusco; 
     customer.DisplayMemberPath = "name"; 
     customer.SelectedValuePath = "customerID"; 
     customer.SelectedValue = "1"; 
    } 

Non si verifica alcun errore ma la casella combinata è sempre vuota. Cusco è la proprietà della mia lista. Non ho idea di cosa non vada con questo codice. Potete aiutarmi?

saluta

public class Customer 
{ 
    public int customerID { get; set; } 
    public string name { get; set; } 
    public string surname { get; set; } 
    public string telnr { get; set; } 
    public string email { get; set; } 
    public string adress { get; set; } 
} 

questa è la classe Customer, che è il mio modello.

public class CustomerViewModel 
{ 
    private ObservableCollection<Customer> _customer; 

    public ObservableCollection<Customer> Customer 
    { 
     get { return _customer; } 
     set { _customer = value; } 
    } 

    public CustomerViewModel() 
    { 
     GetCustomerCollection(); 
    } 

    private void GetCustomerCollection() 
    { 
     Customer = new ObservableCollection<Customer>(BusinessLayer.getCustomerDataSet()); 
    } 

} 

e questo è il ViewModel.

+0

Puoi pubblicare la classe 'Cliente'? –

+1

Hai confermato che c'è qualcosa nell'Elenco che stai fornendo a ItemsSource (nel momento in cui viene impostato, dal momento che non hai questa impostazione come associazione)? – Tim

risposta

23

Prova a impostare la proprietà ItemsSource con un oggetto Binding reale

XAML Method (consigliato):

<ComboBox 
    ItemsSource="{Binding Customer}" 
    SelectedValue="{Binding someViewModelProperty}" 
    DisplayMemberPath="name" 
    SelectedValuePath="customerID"/> 

metodo programmatico:

Binding myBinding = new Binding("Name"); 
myBinding.Source = cusmo.Customer; // data source from your example 

customer.DisplayMemberPath = "name"; 
customer.SelectedValuePath = "customerID"; 
customer.SetBinding(ComboBox.ItemsSourceProperty, myBinding); 

Inoltre, il setter sulla vostra proprietà cliente dovrebbe aumentare l'evento PropertyChanged

public ObservableCollection<Customer> Customer 
{ 
    get { return _customer; } 
    set 
    { 
     _customer = value; 
     RaisePropertyChanged("Customer"); 
    } 
} 

Se quanto sopra non funziona, provare a spostare la porzione di bind dal costruttore al metodo di sovrascrittura OnLoaded. Quando la pagina viene caricata, potrebbe essere il ripristino dei valori.

+1

ok ho provato questo. Ma la casella combinata è vuota. L'ho provato anche nell'evento PageLoaded. – Veeesss

3

come espansione alla risposta di Steve,

è necessario impostare il DataContext del modulo.

Attualmente si dispone di questo:

InitializeComponent(); 
cusmo = new CustomerViewModel(); 
DataContext = this; 

Si deve essere modificato in questo modo:

InitializeComponent(); 
cusmo = new CustomerViewModel(); 
DataContext = cusmo; 

Poi, come ha osservato Steve avrete bisogno di un'altra proprietà sul ViewModel per memorizzare l'elemento selezionato.