c# - WPF Binding in ComboBox with UserControl list -
in 2 combobox , b. a's itemssource custom list. , b's itemssource usercontrol list. when manually setting selecteditem, combobox works well, b combobox ui not show selected item. (in debugging, selecteditem's value mapping right, combobox b's ui not changed.) other structure same between , b. reason?
mainwindow.xaml
... <combobox itemssource="{binding fruitlist}" selecteditem="{binding selectedfruit}" displaymemberpath="fruitname" /> <button content="button" horizontalalignment="left" verticalalignment="top" width="75" click="button_click"/> <combobox itemssource="{binding usercontrollist}" selecteditem="{binding selectedusercontrol}" displaymemberpath="itemname" /> <button content="button" horizontalalignment="left" verticalalignment="top" width="75" click="button_click2"/> </grid>
mainwindow.xaml.cs
public partial class mainwindow : window, inotifypropertychanged { public mainwindow() { initializecomponent(); this.datacontext = this; fruitlist.add(f1); fruitlist.add(f2); fruitlist.add(f3); usercontrollist.add(u1); usercontrollist.add(u2); usercontrollist.add(u3); } fruit f1 = new fruit { fruitname = "apple" }; fruit f2 = new fruit { fruitname = "banana" }; fruit f3 = new fruit { fruitname = "lemon" }; myusercontrol u1 = new myusercontrol { itemname = "apple" }; myusercontrol u2 = new myusercontrol { itemname = "banana" }; myusercontrol u3 = new myusercontrol { itemname = "lemon" }; observablecollection<fruit> _fruitlist = new observablecollection<fruit>(); public observablecollection<fruit> fruitlist { { return _fruitlist; } set { _fruitlist = value; onpropertychanged(); } } fruit _selectedfruit; public fruit selectedfruit { { return _selectedfruit; } set { _selectedfruit = value; onpropertychanged(); } } observablecollection<myusercontrol> _usercontrollist = new observablecollection<myusercontrol>(); public observablecollection<myusercontrol> usercontrollist { { return _usercontrollist; } set { _usercontrollist = value; onpropertychanged(); } } myusercontrol _selectedusercontrol; public myusercontrol selectedusercontrol { { return _selectedusercontrol; } set { _selectedusercontrol = value; onpropertychanged(); } } public event propertychangedeventhandler propertychanged; void onpropertychanged([callermembername] string caller = "") { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(caller)); } } private void button_click(object sender, routedeventargs e) { this.selectedfruit = f3; } private void button_click2(object sender, routedeventargs e) { this.selectedusercontrol = u3; } } public class fruit { public string fruitname { get; set; } }
}
usercontrol
public partial class myusercontrol : usercontrol { public myusercontrol() { initializecomponent(); } public string itemname { get; set; } }
this not way of achieving this. better define itemtemplate combobox have usercontrol in like:
<combobox itemssource="{binding itemlist}" selecteditem="{binding selecteditem}" > <combobox.itemtemplate> <datatemplate> <mycontrols:myusercontrol/> </datatemplate> </combobox.itemtemplate> </combobox>
and define class item
public class item { public string itemname { get; set; } } observablecollection<item> _itemslist = new observablecollection<item>(); public observablecollection<item> itemslist { { return _itemslist ; } set { _itemslist = value; onpropertychanged(); } }
here datacontext of usercontrol item
object. can bind itemname within user control show in anyway want.
in user control can have:
<textblock text="{binding itemname}"></textblock>
Comments
Post a Comment