wpf - Context Menu Binding to Parent Window's Datacontext -



wpf - Context Menu Binding to Parent Window's Datacontext -

i have treelistcontrol binds collection in vm. want define context menu within treelistcontrol having header text bind string in vm. how can set info context in case? tried

<window.datacontext> <model:viewmodel></model:viewmodel> </window.datacontext> <grid> <button grid.row="1" command="{binding cellcheckedcommand}"></button> <textblock text="{binding headertext}" grid.row="2"> <textblock.contextmenu> <contextmenu> <menuitem datacontext="{binding relativesource={relativesource ancestortype={x:type window}}, path=datacontext}" header="{binding headertext}"></menuitem> </contextmenu> </textblock.contextmenu> </textblock> </grid>

but doesn't work.

here viewmodel

public delegatecommand cellcheckedcommand { get; set; } private string _headertext; public string headertext { { homecoming _headertext; } set { _headertext = value; notifypropertychanged("headertext"); } } public void notifypropertychanged(string name) { if(propertychanged != null) { propertychanged(this, new propertychangedeventargs(name)); } } private void cellcheckedmethod() { headertext = "changed"; }

provide name window , explicitly bind such as

<window x:name="reportspage"/> ... <menuitem datacontext="{binding elementname=reportspage}"/>

update

since context menu in own window, binding bit trickier. hence best bet walk relativesource context's parent , pull header text there:

<window.datacontext> <local:mainvm headertext="jabberwocky" /> </window.datacontext> ... <textblock text="{binding headertext}"> <textblock.contextmenu> <contextmenu> <menuitem header="{binding path=parent.datacontext.headertext, relativesource={relativesource self}}" /> </contextmenu> </textblock.contextmenu>

which context produces this

wpf c#-4.0 binding datacontext

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -