wpf combobox binding is empty -
i trying work out wpf difficulties. combobox
seems basic issue can't have populated after reading possible similar post.
the difficulty think combobox
defined in resource, here resource code:
<resourcedictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:diagramdesigner"> <resourcedictionary.mergeddictionaries> <resourcedictionary source="styles/shared.xaml"/> <resourcedictionary source="styles/toolbar.xaml"/> </resourcedictionary.mergeddictionaries> <toolbar x:key="mytoolbar" height="120"> <!--languages--> <groupbox header="localization" style="{staticresource toolbargroup}" margin="3"> <grid> <combobox height="23" horizontalalignment="center" verticalalignment="top" width="120" itemssource="{binding _langliststring}" displaymemberpath="valuestring" selectedvaluepath="valuestring" selectedvalue="{binding langstring}" /> </grid> </groupbox> </toolbar>
my data object defined follow:
public partial class window1 : window { list<comboboxitemstring> _langliststring = new list<comboboxitemstring>(); // object bind combobox selections to. private viewmodelstring _viewmodelstring = new viewmodelstring(); public window1() { // localization settings _langliststring.add(new comboboxitemstring()); _langliststring[0].valuestring = "en-gb"; _langliststring.add(new comboboxitemstring()); _langliststring[1].valuestring = "fr-fr"; _langliststring.add(new comboboxitemstring()); _langliststring[2].valuestring = "en-us"; // set data context window. datacontext = _viewmodelstring; initializecomponent(); }
and modelview:
/// class provides object fill combobox /// can bound string fields in binding object. public class comboboxitemstring { public string valuestring { get; set; } } //______________________________________________________________________ //______________________________________________________________________ //______________________________________________________________________ /// class used bind combobox selections to. must implement /// inotifypropertychanged in order data binding /// work correctly. public class viewmodelstring : inotifypropertychanged { /// need void constructor in order use object element /// in xaml. public viewmodelstring() { } private string _langstring = "en-gb"; /// string property used in binding examples. public string langstring { { return _langstring; } set { if (_langstring != value) { _langstring = value; notifypropertychanged("langstring"); } } } #region inotifypropertychanged members /// need implement interface in order data binding /// work properly. private void notifypropertychanged(string propertyname) { if (propertychanged != null) { propertychanged(this, new propertychangedeventargs(propertyname)); } } public event propertychangedeventhandler propertychanged; #endregion }
i don't know try else. has idea of going on, , why combobox stays empty?
many thanks.
you can bind public properties
itemssource="{binding _langliststring}"
can not work because _langliststring not public property
Comments
Post a Comment