mvvm - ContextMenu too-wide WPF -
i have built contextmenu dynamically using mvvm. problem : content of menuitems in right side ==> wide contextmenu. have ideea problem? thanks
here code in xaml:
<treeview.contextmenu> <contextmenu name="rightclickmenu" itemssource="{binding path=selecteditem.menuitemslist}"> <contextmenu.itemtemplate > <datatemplate> <!-- <menuitem horizontalalignment="left" header="{binding name}" command="{binding command}" --> <stackpanel orientation="horizontal"> <image source="{binding myicon}" width="18" height="18" snapstodevicepixels="true" /> <menuitem header="{binding name}" command="{binding command}" commandparameter="{binding relativesource={relativesource findancestor, ancestortype={x:type treeview}}, path=datacontext.selecteditem}" /> </stackpanel> </datatemplate> </contextmenu.itemtemplate> </contextmenu> </treeview.contextmenu>
it looks :
i want contextmenu this:
the second problem:
sometimes works these strange things.
------------------------------solved--------------------------------------------
for sac1. have modified solution adding x:shared="false". check msdn x:shared.
<style targettype="menuitem" x:shared="false"> <setter property="icon"> <setter.value> <image source="{binding path=myicon}" height="20" width="20" > </image> </setter.value> </setter> <setter property="header" value="{binding path=name}" /> <setter property="command" value="{binding command}" /> <setter property="commandparameter" value="{binding relativesource={relativesource findancestor, ancestortype={x:type treeview}}, path=datacontext.selecteditem}" /> </style>
for wrong headers of menu items had override method tostring() in menuitemviewmodel. don't undestand why had override tostring() works now.
public class menuitemviewmodel : bindablebase { ...... public string name { { return model.name; } set { this.model.name = value; onpropertychanged("name"); } } public override string tostring() { return name; } .... }
datatemplate
of menuitem
not working expected. used style insted of datatemplate:
<treeview.resources> <style targettype="menuitem"> <setter property="icon" value="{binding myicon}" /> <setter property="header" value="{binding name}" /> <setter property="command" value="{binding command}" /> <setter property="commandparameter" value="{binding relativesource={relativesource findancestor, ancestortype={x:type treeview}}, path=datacontext.selecteditem}" /> </style> </treeview.resources> <treeview.contextmenu> <contextmenu itemssource="{binding path=selecteditem.menuitemslist}" /> </treeview.contextmenu>
Comments
Post a Comment