如何将依赖属性绑定到 WPF 中的窗口资源?
假设我有一个上下文菜单作为窗口资源:
<Window.Resources>
<ContextMenu
x:Shared="false"
x:Key="ContextMenu">
<MenuItem Header="{??}" />
</ContextMenu>
</Window.Resources>
并且我想将 MenuItem.Header
绑定到窗口上名为 MenuItemHeader
的依赖属性。 {??}
应该是什么?窗口类后面的代码应该是什么样子?
Suppose I have a context menu as a window resource:
<Window.Resources>
<ContextMenu
x:Shared="false"
x:Key="ContextMenu">
<MenuItem Header="{??}" />
</ContextMenu>
</Window.Resources>
And I want to bind MenuItem.Header
to a dependency property called MenuItemHeader
on the window. What should {??}
be and what should the code behind in the window class look like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以命名该窗口并引用它的
MyDp
作为属性。如果您不知道如何定义依赖项属性,请阅读概述 。(我会使用
Source
和x:引用
为ElementName
通常在诸如ContextMenus
这样的断开连接的地方不起作用)You could name the window and reference it
MyDp
being the property. If you don't know how to define a dependency property read the overview.(I would use
Source
andx:Reference
asElementName
usually does not work in disconnected places likeContextMenus
)