wpf - TemplateBinding 的限制

标签 wpf templatebinding

自定义控件(在 VS 2008 下编写)具有 SelectedColor 依赖属性,其控件模板包含以下内容:

...
<Rectangle>
  <Rectangle.Fill>
    <SolidColorBrush Color="{TemplateBinding SelectedColor}"/>
  </Rectangle.Fill>
</Rectangle>
...

除非将绑定(bind)替换为:

...
<Rectangle>
  <Rectangle.Fill>
    <SolidColorBrush Color="{Binding SelectedColor, RelativeSource={RelativeSource TemplatedParent}}"/>
  </Rectangle.Fill>
</Rectangle>
...

好的,我知道 TemplateBinding 是 Binding 的简化版本,它有一组限制,那么导致上述代码不起作用的确切限制是什么?

最佳答案

TemplateBinding 非常不同。应用模板时将它们视为简单的值分配。
由于您 SelectedItem 在运行时更改,因此您需要一个真正的属性更改通知绑定(bind)。

关于wpf - TemplateBinding 的限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6442175/

相关文章:

c# - 笔刷动画

c# - 在 XAML 中使用矩形作为剪辑

xaml - 是否可以在 XAML 中的 TemplateBinding 上使用转换器?

WPF DataTemplate 属性设置在 Content

c# - 来自主窗口文本框的用户控件文本框内的 wpf 绑定(bind)文本

C#、WPF - OpenFileDialog 不出现

控件模板中的 WPF 嵌套绑定(bind)

c# - 如何将TemplateBinding应用到 "Auto"高宽

animation - 是否可以在 Silverlight 的 Storyboard 中使用 TemplateBinding?

wpf - 将不相关的属性绑定(bind)到 DataGrid