4

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

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

除非将绑定替换为:

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

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

4

1 回答 1

2

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

于 2011-09-12T17:55:54.763 回答