【问题标题】:How to bind a dependency property to a window resource in WPF?如何将依赖属性绑定到 WPF 中的窗口资源?
【发布时间】:2012-02-16 15:19:23
【问题描述】:

假设我有一个上下文菜单作为窗口资源:

<Window.Resources>
    <ContextMenu 
        x:Shared="false"
        x:Key="ContextMenu">
        <MenuItem Header="{??}" />
    </ContextMenu>
</Window.Resources>

我想将MenuItem.Header 绑定到窗口上名为MenuItemHeader 的依赖属性。 {??}应该是什么,window类后面的代码应该是什么样的?

【问题讨论】:

    标签: c# wpf xaml data-binding dependency-properties


    【解决方案1】:

    您可以命名窗口并引用它

    <Window Name="Window" ...>
    
    <MenuItem Header="{Binding MyDp, Source={x:Reference Window}}"...>
    

    MyDp 是财产。如果您不知道如何定义依赖属性,请阅读 the overview。

    (我会使用Source 和x:Reference 作为ElementName 通常在ContextMenus 等断开连接的地方不起作用)

    【讨论】:

      猜你喜欢
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      相关资源
      最近更新 更多