Saturday, 15 September 2012

mvvm - WPF ComboBox not Firing on selection -



mvvm - WPF ComboBox not Firing on selection -

i have combobox, shown below. why isn't code-behind firing?

xaml:

class="lang-xml prettyprint-override"><combobox height="23" name="cbappendcreate" verticalalignment="top" width="120" margin="{staticresource consistentmargins}" itemssource="{binding path=cbcreateappenditems}" selectedvalue="{binding path=cbappendcreate,updatesourcetrigger=propertychanged}" />

codebehind:

class="lang-cs prettyprint-override">private string cbappendcreate; public string cbappendcreate { { //.... homecoming cbappendcreate } set { //this doesn't fire when selecting first of 2 items, //but fires when selecting 2nd of 2 items //.... cbappendcreate = value; } }

i'll post working code here, it's simple. i've created default wpf app using vs2012 template. here's mainwindow.xaml content:

<window x:class=" wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <stackpanel> <combobox height="23" name="cbappendcreate" verticalalignment="top" width="120" itemssource="{binding path=cbcreateappenditems}" selectedvalue="{binding path=cbappendcreate,updatesourcetrigger=propertychanged}" /> <textblock text="{binding cbappendcreate}"></textblock> </stackpanel>

here's code-behind:

namespace wpfapplication1 { public class datasource { public list<string> cbcreateappenditems { get; set; } public string cbappendcreate { get; set; } public datasource() { cbcreateappenditems = new list<string>() { "create", "append" }; } } /// <summary> /// interaction logic mainwindow.xaml /// </summary> public partial class mainwindow : window { public mainwindow() { initializecomponent(); datacontext = new datasource(); } } }

when select different values in combo-box, textblock updates same value, hence vm's property updated.

wpf mvvm binding

No comments:

Post a Comment