vb.net - WPF Data Binding and Templates -
i working on project , want have list box custom data template populate user data. question is, when click on item in list box, how can tell item selected? basically, if select "kevin", want display data. if select dave, want display data. not know how data our after bound...
edit: found great tutorial covers this. hidden gem.
bind selecteditem of combobox property.
<window x:class="comboboxselecteditembinding.window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="window1" height="300" width="500"> <grid> <grid.columndefinitions> <columndefinition></columndefinition> <columndefinition></columndefinition> </grid.columndefinitions> <listbox x:name="st" itemssource="{binding path=customers,mode=twoway}" issynchronizedwithcurrentitem="true" selecteditem="{binding path=selectedcustomer,mode=twoway}" margin="0,38,0,80"> <listbox.itemtemplate> <datatemplate> <textblock text="{binding path=name}"></textblock> </datatemplate> </listbox.itemtemplate> </listbox> <textblock text="{binding path=selectedcustomer.name}" grid.column="1" verticalalignment="center" margin="5"></textblock> </grid>
public partial class window1 : window, inotifypropertychanged { private observablecollection<customer> customers; public observablecollection<customer> customers { { return customers; } set { customers = value; notifypropertychanged("customers"); } } private customer selectedcustomer; public customer selectedcustomer { { return selectedcustomer; } set { selectedcustomer = value; notifypropertychanged("selectedcustomer"); } } public window1() { customers = new observablecollection<customer>(); customers.add(new customer() { id = 1, name = "ravi", salary = 1000 }); customers.add(new customer() { id = 99, name = "alex", salary = 3000 }); customers.add(new customer() { id = 123, name = "steve", salary = 100 }); customers.add(new customer() { id = 31, name = "alice", salary = null }); initializecomponent(); datacontext = this; } #region inotifypropertychanged members public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string info) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(info)); } } #endregion } public class customer:inotifypropertychanged { private int id; public int id { { return id; } set { id = value; notifypropertychanged("id"); } } private string name; public string name { { return name; } set { name = value; notifypropertychanged("name"); } } private decimal? salary; public decimal? salary { { return salary; } set { salary = value; notifypropertychanged("salary"); } } #region inotifypropertychanged members public event propertychangedeventhandler propertychanged; private void notifypropertychanged(string info) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(info)); } } #endregion }
Comments
Post a Comment