【发布时间】:2018-12-08 07:28:20
【问题描述】:
我的目标是将 XAML 中的元素属性绑定到类背后代码的属性,而 DataContext 仍然是 ViewModel。
原因是,我在 XAML 中只有一些 UI 修饰属性,这些属性不受 ViewModel 控制,而是由背后的代码控制。
所以基本上我搜索的是这样的:
<Element
Attribute = "{Binding ThatOneCodeBehind.WhateverProperty}"
OtherAttribute1 = "{Binding StillDataContextSomething}"
OtherAttribute2 = "{Binding StillDataContextSomething}"
/>
Attribute="{Binding ThatOneCodeBehind:WhateverProperty}" 的正确绑定语法是什么?
【问题讨论】:
-
“ThatOneCodeBehind.WhateverProperty”是包含“元素”控件的主控件的 DependencyProperty 吗?
-
给那个元素一个名字,你就可以从代码中访问它。例如:
<Element x:Name="Test" ...来自代码:Test.Attribute = WhateverYouWant -
假设您所指的“代码背后”是一个窗口,确保无论什么属性是窗口类中的公共属性,并像
{Binding WhateverProperty, RelativeSource={RelativeSource AncestorType=Window}}一样绑定。
标签: wpf xaml data-binding code-behind