c# - How to change ComboBox SelectedValue programmatically with MVVM -
i have combobox items a, b, c, d, e.
how can change selectedvalue of combobox after user selection, if user select list items "a", selectedvalue "d" (as if selected d himself).
xaml:
<stackpanel orientation="horizontal"> <textbox text="{binding path=name, updatesourcetrigger=propertychanged, mode=twoway}" height="25" width="100" /> <combobox isdropdownopen="{binding isdropdownopen, mode=twoway, updatesourcetrigger=propertychanged}" itemssource="{binding offsetvalues}" selectedvalue="{binding nodecategory, mode=twoway}" height="25" width="100" ishittestvisible="false" background="aliceblue"> <combobox.resources> <sys:double x:key="{x:static systemparameters.verticalscrollbarwidthkey}">0</sys:double> </combobox.resources> </combobox> </stackpanel>
viewmodel:
class viewmodel : viewmodelbase { private ilist<string> offsetvalues = new list<string>() { "mv", "v" }; public ilist<string> offsetvalues { { return offsetvalues; } set { offsetvalues = value; } } private bool isdropdownopen; public bool isdropdownopen { { return isdropdownopen; } set { isdropdownopen = value; onpropertychanged(); } } private string _name; public string name { { return _name; } set { _name = value; onpropertychanged( "name" ); if( _name != "" ) { isdropdownopen = true; onpropertychanged( "isdropdownopen" ); } } } private string _nodecategory; public string nodecategory { { return _nodecategory; } set { if( convert.todouble( _name ) > 1000 ) { _name = "1.0"; onpropertychanged( "name" ); _nodecategory = offsetvalues[1]; onpropertychanged( "nodecategory" ); } else { _nodecategory = value; onpropertychanged( "nodecategory" ); } } } } public class viewmodelbase : inotifypropertychanged { protected virtual void onpropertychanged( [callermembername]string propertyname = null ) { propertychanged.invoke( this, new propertychangedeventargs( propertyname ) ); } public event propertychangedeventhandler propertychanged; }
thanks
add xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
than can invoke command when selecteditemchanged like
<combobox x:name="nodecategoriescombobox"> <i:interaction.triggers> <i:eventtrigger eventname="selectionchanged"> <i:invokecommandaction command="{binding updatenodecategorycommand}" commandparameter="{binding elementname=nodecategoriescombobox, path=selectedvalue}"/> </i:eventtrigger> </i:interaction.triggers> </combobox>
than add updatenodecategorycommand , update nodecategory property
private relaycommand<string> _updatenodecategorycommand ; public relaycommand<string> updatenodecategorycommand { { return _updatenodecategorycommand ?? (_updatenodecategorycommand = new relaycommand<string>( nodecategory => { nodecategory=nodecategory })); } }
Comments
Post a Comment