wpf 上下文菜单绑定到父窗口的数据上下文

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26359091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 12:36:39  来源:igfitidea点击:

Context Menu Binding to Parent Window's Datacontext

wpfc#-4.0bindingdatacontext

提问by Helic

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

我有一个 TreeListControl 绑定到我的 VM 中的集合。我还想在 treelistcontrol 中定义上下文菜单,使其标题文本绑定到我的 VM 中的另一个字符串。在这种情况下如何设置数据上下文?我试过了

<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";
}

采纳答案by ΩmegaMan

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 RelativeSourceto the context's parent and pull the header text from there:

由于上下文菜单实际上在其自己的窗口中,因此绑定有点棘手。因此,最好的办法是走到RelativeSource上下文的父级并从那里拉出标题文本:

    <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

对于这种情况,这会产生

enter image description here

在此处输入图片说明

回答by lisp

This binds to a Window:

这绑定到一个Window

DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"

If the command AddItemCommandand property AddItemTextare defined on the WindowViewModel, bind to WindowDataContext:

如果命令AddItemCommand和属性AddItemText在 上定义,则WindowViewModel绑定到WindowDataContext

DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext}"