【问题标题】:In Button Click Context Menu How do I bind to viewModel?在按钮单击上下文菜单中如何绑定到 viewModel?
【发布时间】:2014-02-06 06:22:59
【问题描述】:

我有一个单击该按钮打开上下文菜单的按钮,现在单击上下文菜单将绑定到 viewModel。但它没有发生。

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
    <Button.ContextMenu>
        <ContextMenu>
           <MenuItem Header="Copy Download link " Command="{Binding Path=Parent.PlacementTarget.Tag.CopyViewCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}" />
           <MenuItem ... />
        </ContextMenu>
    </Button.ContextMenu> 
</Button>

我已经尝试过 tag 属性,但在我看来它不起作用。如果我绑定到按钮本身,视图模型工作正常,但 contextMenu 数据绑定不起作用。

编辑:

现在代码经过讨论可以正常工作,我想把它贴在这里。

我所做的更改是我把 UpdateSourceTrigger="Propertychanged" 这里是代码

<Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
    <Button.ContextMenu>
       <ContextMenu Width="{Binding RelativeSource={RelativeSource Self}}">
          <MenuItem Header="Copy View link " Command="{Binding CopyViewCommand, UpdateSourceTrigger=PropertyChanged}" />
          <MenuItem ... />
       </ContextMenu>
    </Button.ContextMenu> 
</Button>

但是我不知道它是怎么突然起作用的,在按钮上下文菜单的情况下它必须与标签属性一起使用。如果有人对此有所了解,我想很多像我这样的新 WPF 和数据绑定的人都会受益。

【问题讨论】:

    标签: c# wpf data-binding mvvm contextmenu


    【解决方案1】:

    我假设您在UserControl 中使用此按钮。请尝试以下代码

    <Button Content="Copy" Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
            <Button.ContextMenu>
                <ContextMenu>
                   <MenuItem Header="Copy Download link " Command="{Binding RelativeSource={RelativeSource FindAncestor,  AncestorType={x:Type UserControl}}, Path=DataContext.CopyViewCommand}" />
                   <MenuItem ... />
                </ContextMenu>
            </Button.ContextMenu> 
        </Button>
    

    【讨论】:

    • 我从按钮中删除了命令并尝试了您的代码,但不起作用
    • 您不需要从按钮中删除命令。请检查我的代码 sn-p。
    【解决方案2】:
    <Button Content="Copy" Command="{Binding LinkCopyCommand, UpdateSourceTrigger=PropertyChanged}" >
    <Button.ContextMenu>
        <ContextMenu>
           <MenuItem Header="Copy Download link " Command="{Binding Path=CopyViewCommand}" />
           <MenuItem ... />
        </ContextMenu>
    </Button.ContextMenu> 
    

    CopyViewCommand 直接从您的 DataContext... 绑定,这是您的 ViewModel..

    【讨论】:

    • 让我理解代码,在 Button Tag="{Binding LinkViewModel, RelativeSource={RelativeSource Mode=Self}}" 所以标签会绑定到 viewModel 对吗?然后 这绑定到viewmodel CopyViewCommand,但更新后它不起作用代码。
    • 我认为问题就在这里.. LinkViewModel 是否存在于 Button.. 因为您的来源是 Button(Control)
    • LinkViewModel 属性在哪里可用?
    • 请看我更新的代码,你能简要介绍一下为什么它现在可以工作。我尝试使用标签属性,但没有它,它工作正常。是的,LinkViewModel 已附加到 Button。
    【解决方案3】:

    您必须将 ContextMenu 的 DataContext 设置为您的 ViewModel。一种方法是为上下文菜单设置一个 Opened 事件处理程序。

    在下面的链接中查看我的答案 -

    Context Menu items command binding WPF using MVVM

    【讨论】:

      猜你喜欢
      • 2016-07-14
      • 2017-02-12
      • 2017-12-04
      • 2013-09-23
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2017-09-04
      相关资源
      最近更新 更多