2012-06-15 12 views
8

Witam Próbuję powiązać listę <> do combobox.Wpf combobox binding

<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"; 
    } 

Nie otrzymuję błędu, ale pole Combobox jest zawsze puste. Cusco jest własnością mojej listy. Nie mam pojęcia, co jest złego w tym kodzie. Czy możesz mi pomóc?

Greets

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; } 
} 

to klasa klienta, który jest mój model.

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()); 
    } 

} 

i jest to ViewModel.

+0

Czy możesz opublikować klasę "Klient"? –

+1

Czy potwierdziłeś, że na liście jest coś, co podajesz do ItemsSource (w momencie, w którym zostało ustawione, ponieważ nie masz tej konfiguracji jako wiązania)? – Tim

Odpowiedz

23

Spróbuj ustawić właściwość ItemsSource o rzeczywistej Binding obiektu

XAML Method (zalecane):

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

programowa metoda:

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); 

Również seter na nieruchomości klient powinien Podnieście wydarzenie PropertyChanged

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

Jeśli powyższe nie działa, spróbuj przenieść część wiążącą z konstruktora do metody OverLide OnLoaded. Po załadowaniu strony może ona resetować Twoje wartości.

+1

ok próbowałem tego. Ale combobox jest pusty. Próbowałem również w wydarzeniu PageLoaded. – Veeesss

3

Jako ekspansji na odpowiedź Steve'a,

Musisz ustawić datacontext swojej postaci.

Obecnie masz to:

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

powinno zostać zmienione w ten sposób:

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

Potem jak zauważył Steve trzeba będzie inny obiekt na viewmodel aby zapisać wybrany element.