c# - How to properly set a Datagrid's ContextMenu's databinding (MVVM Light) -
can please take @ xaml , please tell me why command on menuitem not work. assume there error in datagrid's tag binding, however, can't seem wrap head around going on.
on side note understand context menus not share same visual tree. understand if set contextmenu datacontext of window, works expected. i'm trying understand why placementtarget.tag binding not work expected. in advance.
the view looks like:
<window x:class="wpfapplication8.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:wpfapplication8" xmlns:diag="clr-namespace:system.diagnostics;assembly=windowsbase" mc:ignorable="d" title="mainwindow" height="350" width="525" datacontext="{binding source={staticresource locator}, path=main}"> <grid> <datagrid itemssource="{binding people}" autogeneratecolumns="false" tag="{binding relativesource={relativesource mode=findancestor, ancestortype={x:type window}}}"> <datagrid.resources> <contextmenu x:key="context" datacontext="{binding placementtarget.tag, relativesource={relativesource self}}"> <menuitem header="open" command="{binding opencommand, diag:presentationtracesources.tracelevel=high}"/> </contextmenu> </datagrid.resources> <datagrid.columns> <datagridtextcolumn header="first" binding="{binding firstname}"/> </datagrid.columns> <datagrid.rowstyle> <style targettype="{x:type datagridrow}"> <setter property="contextmenu" value="{staticresource context}"/> </style> </datagrid.rowstyle> </datagrid> </grid> </window>
the viewmodel
public class mainviewmodel : viewmodelbase { /// <summary> /// initializes new instance of mainviewmodel class. /// </summary> public mainviewmodel() { if (isindesignmode) { // code runs in blend --> create design time data. } else { // code runs "for real" people = new observablecollection<person> { new person { firstname = "tim", }, new person { firstname = "todd" }, new person { firstname = "jane" }, new person { firstname = "doe" }, }; } } public observablecollection<person> people { get; set; } private relaycommand _opencommand; /// <summary> /// gets opencommand. /// </summary> public relaycommand opencommand { { return _opencommand ?? (_opencommand = new relaycommand( () => { debug.writeline("yes!"); })); } } }
and model
public class person { public string firstname { get; set; } }
and debug info
system.windows.data warning: 56 : created bindingexpression (hash=18243409) binding (hash=24719867) system.windows.data warning: 58 : path: 'opencommand' system.windows.data warning: 60 : bindingexpression (hash=18243409): default mode resolved oneway system.windows.data warning: 61 : bindingexpression (hash=18243409): default update trigger resolved propertychanged system.windows.data warning: 62 : bindingexpression (hash=18243409): attach system.windows.controls.menuitem.command (hash=668104) system.windows.data warning: 67 : bindingexpression (hash=18243409): resolving source system.windows.data warning: 70 : bindingexpression (hash=18243409): found data context element: menuitem (hash=668104) (ok) system.windows.data warning: 71 : bindingexpression (hash=18243409): datacontext null system.windows.data warning: 65 : bindingexpression (hash=18243409): resolve source deferred system.windows.data warning: 67 : bindingexpression (hash=18243409): resolving source system.windows.data warning: 70 : bindingexpression (hash=18243409): found data context element: menuitem (hash=668104) (ok) system.windows.data warning: 71 : bindingexpression (hash=18243409): datacontext null system.windows.data warning: 67 : bindingexpression (hash=18243409): resolving source system.windows.data warning: 70 : bindingexpression (hash=18243409): found data context element: menuitem (hash=668104) (ok) system.windows.data warning: 71 : bindingexpression (hash=18243409): datacontext null system.windows.data warning: 67 : bindingexpression (hash=18243409): resolving source system.windows.data warning: 70 : bindingexpression (hash=18243409): found data context element: menuitem (hash=668104) (ok) system.windows.data warning: 71 : bindingexpression (hash=18243409): datacontext null system.windows.data warning: 67 : bindingexpression (hash=18243409): resolving source system.windows.data warning: 70 : bindingexpression (hash=18243409): found data context element: menuitem (hash=668104) (ok) system.windows.data warning: 71 : bindingexpression (hash=18243409): datacontext null system.windows.data warning: 67 : bindingexpression (hash=18243409): resolving source (last chance) system.windows.data warning: 70 : bindingexpression (hash=18243409): found data context element: menuitem (hash=668104) (ok) system.windows.data warning: 78 : bindingexpression (hash=18243409): activate root item <null> system.windows.data warning: 106 : bindingexpression (hash=18243409): item @ level 0 null - no accessor system.windows.data warning: 80 : bindingexpression (hash=18243409): transfervalue - got raw value {dependencyproperty.unsetvalue} system.windows.data warning: 88 : bindingexpression (hash=18243409): transfervalue - using fallback/default value <null> system.windows.data warning: 89 : bindingexpression (hash=18243409): transfervalue - using final value <null>
Comments
Post a Comment