自定义控件(在 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/