4

A custom control (written under VS 2008) has a SelectedColor Dependency Property and its Control Template contains the following:

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

The rectangle does not have the correct color unless the binding is repleaced with:

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

OK, I am aware that TemplateBinding is a simplified version of Binding and it has a set of limitations, so what is the exact limitation that cause the above code not to work?

Ury Jamshy
  • 173
  • 6
  • According to [MSDN](http://msdn.microsoft.com/en-us/library/ms742882.aspx) those are 2 analogous notations. Did you try with explicit `Path=` – Philippe Lavoie Jun 22 '11 at 15:35
  • 3
    I've run into this before, too. I never could figure out the specific differences apart from `TemplateBinding` not supporting nested paths. So I just use `TemplateBinding` by default (better performance) and switch to `Binding` if it doesn't work. Annoying, but that's my process. – Kent Boogaart Jun 22 '11 at 15:37
  • 1
    @Philippe Lavoie - The problem lies in the use of TemplateBinding, other notation fails as well. – Ury Jamshy Jun 23 '11 at 08:20
  • 1
    The difference between both notations is that TemplateBinding is processed at compiletime and the Binding with a relative source at runtime. You can have a look at this post: http://stackoverflow.com/questions/1131222/wpf-templatebinding-vs-relativesource-templatedparent Though this doesn't explain your problem. – MatthiasG Sep 08 '11 at 10:06

1 Answers1

2

TemplateBinding are very different. Think of them as simple value assignment when the template is applied. Since you SelectedItem changes at runtime, you need a real binding for property change notification.

Alex Maker
  • 1,529
  • 2
  • 19
  • 27