上下文菜单绑定到父窗口的的DataContext [英] Context Menu Binding to Parent Window's Datacontext
问题描述
我有绑定到我的虚拟机集合的TreeListControl。我也想定义有其标题文字绑定到我的虚拟机另一个字符串的treelistcontrol内的上下文菜单。我怎么可以设置在这种情况下,数据上下文?我试图
< Window.DataContext>
<型号:视图模型>< /型号:视图模型>
< /Window.DataContext>
<网格和GT;
<按钮Grid.Row =1命令={结合CellCheckedCommand}>< /按钮> < TextBlock的文本={结合的HeaderText}Grid.Row =2>
< TextBlock.ContextMenu>
<&文本菜单GT;
<菜单项的DataContext ={绑定的RelativeSource = {的RelativeSource AncestorType = {X:类型窗口}},路径=的DataContext}标题={结合的HeaderText}>< /菜单项>
< /文本菜单>
< /TextBlock.ContextMenu>
< / TextBlock的>
< /网格和GT;
,但它不工作。
下面是视图模型
公共DelegateCommand CellCheckedCommand {搞定;组; }私人字符串_HeaderText;公共字符串的HeaderText
{
得到
{
返回_HeaderText;
}
组
{
_HeaderText =价值;
NotifyPropertyChanged(的HeaderText);
}
}公共无效NotifyPropertyChanged(字符串名称)
{
如果(的PropertyChanged!= NULL)
{
的PropertyChanged(这一点,新PropertyChangedEventArgs(名));
}
}私人无效CellCheckedMethod()
{
的HeaderText =更改;
}
为你的窗口提供一个名称,并明确绑定到它,如
<窗口x:名称=ReportsPage/>... <菜单项的DataContext ={绑定的ElementName = ReportsPage}/>
更新
由于上下文菜单实际上是在自己的窗口,绑定是有点棘手。因此,最好的办法是走了的RelativeSource
上下文的父,并从那里拉头文本:
< Window.DataContext>
<局部:MainVM的HeaderText =Jabberwocky/>
< /Window.DataContext> ...< TextBlock的文本={结合的HeaderText}>
< TextBlock.ContextMenu>
<&文本菜单GT;<菜单项标题={绑定路径= Parent.DataContext.HeaderText,
的RelativeSource = {自我的RelativeSource}}/> < /文本菜单>
< /TextBlock.ContextMenu>
这对于这样的背景下产生的这种
I have a TreeListControl that binds to a collection in my VM. I also want to define the context menu inside the treelistcontrol having its header text bind to another string in my VM. how can I set the data context in this case? I tried to
<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 it doesn't work.
Here is the ViewModel
public DelegateCommand CellCheckedCommand { get; set; }
private String _HeaderText;
public String HeaderText
{
get
{
return _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 a name for your window and explicitly bind to it such as
<window x:Name="ReportsPage"/>
...
<MenuItem DataContext="{Binding ElementName=ReportsPage}"/>
UPDATE
Since the context menu is actually in its own window, binding is a bit trickier. Hence the best bet is to walk up the RelativeSource
to the context's parent and pull the header text from 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 for this context produces this
这篇关于上下文菜单绑定到父窗口的的DataContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!