【问题标题】:Toolbar with ItemsSource binding can't bind to main VM具有 ItemsSource 绑定的工具栏无法绑定到主 VM
【发布时间】:2011-03-22 18:39:05
【问题描述】:

我有一个使用 ItemsSource 绑定到我的主 VM 的工具栏。 每个项目都是 ToolBarItem 的一种类型,具有图标、命令等... 我的工具栏命令之一是在我的主 VM(不是 ToolBarItem)上设置 IsOptionsDialogOpen,我将 PopUp.IsOpen(它是我的 ToolBar 模板的一部分)绑定到此属性,但它从不访问它,原因是它是一部分我的 Main VM,而不是 ToolBarItem 类。

我试过这样绑定:

IsOpen="{Binding IsOptionsDialogOpen, RelativeSource={RelativeSource FindAncestor,  AncestorType={x:Type ToolBar}}}"

但这也没有用,有什么想法吗?

【问题讨论】:

    标签: wpf mvvm


    【解决方案1】:

    绑定中的RelativeSource [如果成功] 将返回ToolBar 本身,而不是DataContext。更改绑定:

    IsOpen="{Binding DataContext.IsOptionsDialogOpen, RelativeSource=...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      相关资源
      最近更新 更多