c# - Binding an observable collection to a context menu -
i trying make context menu show list of strings. app contains datagrid of people can edited,deleted , added group. have class studentgroup different groups person can added (one of members of studentgroup name). viewmodel retrieves full list of groups , puts inside observable collection. trying make context menu work such whenever user right clicks , hover on add user -> opens side menuitems contains observable collections's name string.
this have tried far looking similar questions on stackoverflow, hasn't worked me yet.
the xaml:
<datagrid.contextmenu> <contextmenu allowdrop="true" itemssource="{binding entries}"> <menuitem header="edit" /> <menuitem header="delete"/> <menuitem header="add user to"> <menuitem.itemtemplate> <datatemplate> <textblock text="{binding name}" /> </datatemplate> </menuitem.itemtemplate> </menuitem> </contextmenu> </datagrid.contextmenu>
xaml code behind view:
private titleviewmode tvm=new titleviewmode; public welcome() { initializecomponent(); _grid1.contextmenu.datacontext = tvm; }
viewmodel
class titleviewmodel { public observablecollection<studentgroup> entries {get;set;} private list<studentgroup> sg1 { get; set;} public titletviewmodel() { sg1 = getgroups(); entries = new observablecollection<studentgroup>(sg1); } }
this should work
<menuitem header="add user to" itemssource="{binding entries}"> <menuitem.itemtemplate> <datatemplate> <menuitem header="{binding name}"></menuitem> </datatemplate> </menuitem.itemtemplate> </menuitem>
Comments
Post a Comment