c# - WPF ComboBox Set using Grid DataContext -
i new c# wpf
, tring set datacontext
combobox
xml below
<grid name="groupeditarea" horizontalalignment="stretch" verticalalignment="stretch" background="#ffd8d8d8" margin="-14,0,192,0"> <label content="group name" fontsize="18" horizontalalignment="left" margin="116,50,0,0" verticalalignment="top" width="136"/> <label content="group type" fontsize="18" horizontalalignment="left" margin="116,123,0,0" verticalalignment="top" width="136"/> <textbox x:name="groupgroupnametxt" horizontalalignment="left" fontsize="16" height="31" margin="368,50,0,0" textwrapping="wrap" text="{binding path = groupname, mode=twoway, stringformat=\{0:n3\}, updatesourcetrigger=propertychanged}" verticalalignment="top" width="226" textchanged="groupgroupnametxt_textchanged" /> <!-- gotfocus="groupgroupnametxt_ongotfocus" textinput="groupgroupnametxt_ontextinput" --> <combobox x:name="groupgroupnamecombo" horizontalalignment="left" margin="368,123,0,0" verticalalignment="top" width="226" height="31" selectionchanged="groupgroupnamecombo_selectionchanged" displaymemberpath="groupname" selectedvaluepath="categoriesval" selectedvalue="{binding categories}"/> </grid>
my poco
below :-
public class test: inotifypropertychanged { public test() { } public virtual string testid { get; set; } public virtual categories categoriesval { get; set; } public virtual string name{ get; set; } public virtual string groupname { { return name; } set { name = value; onpropertychanged("groupname"); } } } public class categories : inotifypropertychanged { public categories () { } public virtual string categorieid { get; set; } public virtual string name{ get; set; } public virtual string groupname { { return name; } set { name = value; onpropertychanged("groupname"); } } } }
and in backend
code setting datacontext
below :-
categories cate = new categories (); cate.categorieid = "cate1id"; cate.groupname = "categroupname1" test test = new test(); test.testid = "testid"; test.categoriesval = cate; test.name = "testname1";
and groupgroupnamecombo
set using itemssource
, contain entire list on categories
when set using below working fine
groupgroupnamecombo.selecteditem = cate;
but when set using below grid wont work :-
groupeditarea.datacontext = test;
can guide me how can set combobox
setting grid datacontext
instead of setting manually combobox
.
instead
selectedvaluepath="categoriesval" selectedvalue="{binding categories}"
write
selecteditem="{binding categoriesval}"
selectedvaluepath
means: name of property (from comboboxitem datacontex - categories
class in our case) value of selectedvalue
provided.
useful in case want representation of instance-item (each form combobox.items
) not done item itself, 1 characteristic. in case not see point in it.
see more: difference between selecteditem, selectedvalue , selectedvaluepath
Comments
Post a Comment