Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf combobox binding

Hi I´m trying to bind a List<> to a combobox.

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

I become no Error but the Combobox is always empty. Cusco is the Property of my List. I have no idea whats wrong with this code. Can you help me?

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

this is the Customer Class which is my 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());
    }

}

and this is the ViewModel.

like image 675
Veeesss Avatar asked Jun 15 '12 18:06

Veeesss


People also ask

How do I bind a ComboBox?

To bind a ComboBox or ListBox control If you are binding to a table, set the DisplayMember property to the name of a column in the data source. If you are binding to an IList, set the display member to a public property of the type in the list.

What is two way binding WPF?

In two way we can change data in sources control and target control. Target to source and source to target means if we change source control value then it will automatically change target control value. And vice-a versa for source control. That's why it's called two-way data binding.


2 Answers

Try setting the ItemsSource property with an actual Binding object

XAML Method (recommended):

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

Programmatic method:

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

Also, the setter on your Customer property should raise the PropertyChanged event

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

If the above does not work, try moving the binding portion from the constructor to the OnLoaded override method. When the page loads, it may be resetting your values.

like image 198
Steve Konves Avatar answered Oct 04 '22 04:10

Steve Konves


As an expansion on Steve's answer,

You need to set the datacontext of your form.

Currently you have this:

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

It should be changed to this:

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

Then as Steve noted you will need another property on the viewmodel to store the selected item.

like image 27
klaverty Avatar answered Oct 04 '22 04:10

klaverty