I am trying my hands on WPF. I am trying to bind a ComboBox with List<MyClass> where MyClass is user defined class. I want to set SelectedValuePath property of ComboBox so that, value of ComboBox item represents object of MyClass.
I tried setting SelectedValuePath property to . and this, but no luck. Can anybody suggest me the way to achieve it?
Example Code :
Class MyClass
{
public int ID {get; set;}
public string Name {get; set;}
}
List<MyClass> lst = new List<MyClass>();
ComboBox cmb = new ComboBox();
cmb.DataContext = lst;
cmb.DisplayMemberPath = "Name";
// Here I want object of MyClass (which is bound to this item) itself should be assigned as value.
cmb.SelectedValuePath = "????";
Use
cmb.SelectedIteminstead which returns an object, cast it asMyClassand do not set SelectedValuePath at all