Binding a WPF ComboBox to a custom list

后端 未结 4 619
暗喜
暗喜 2020-11-22 11:59

I have a ComboBox that doesn\'t seem to update the SelectedItem/SelectedValue.

The ComboBox ItemsSource is bound to a property on a ViewModel class that lists a bunch

4条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 12:31

    To bind the data to ComboBox

    List ListData = new List();
    ListData.Add(new ComboData { Id = "1", Value = "One" });
    ListData.Add(new ComboData { Id = "2", Value = "Two" });
    ListData.Add(new ComboData { Id = "3", Value = "Three" });
    ListData.Add(new ComboData { Id = "4", Value = "Four" });
    ListData.Add(new ComboData { Id = "5", Value = "Five" });
    
    cbotest.ItemsSource = ListData;
    cbotest.DisplayMemberPath = "Value";
    cbotest.SelectedValuePath = "Id";
    
    cbotest.SelectedValue = "2";
    

    ComboData looks like:

    public class ComboData
    { 
      public int Id { get; set; } 
      public string Value { get; set; } 
    }
    

提交回复
热议问题