Consider this (edited-down) Style
, designed for a Button
whose Content
is a String
:
<Style x:Key="Test" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<StackPanel>
<TextBlock x:Name="text" Text="{TemplateBinding Content}" />
<TextBlock x:Name="demo" Text="{Binding RelativeSource={RelativeSource TemplatedParent}}" />
</StackPanel>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}">
<DataTrigger.Value>
<system:String>Test</system:String>
</DataTrigger.Value>
<Setter TargetName="test" Property="Foreground" Value="Red" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The intention in this example is to turn the button text red if it equals the word "Test"1. But it doesn't work, because the trigger's TemplatedParent
binding resolves to null instead of to the Button
the Style
is applied to. However, the TextBlock
named "demo" will have its Text
set to "System.Windows.Controls.Button: [ButtonText]" as expected, which means TemplatedParent
works correctly at that level. Why doesn't it work inside the DataTrigger
?
1 I know there are other ways to achieve that, but I'm trying to understand why the binding doesn't work the way I expect it to.
DataTrigger
because I am really working with a non-DependencyProperty of the TemplatedParent. – Panettone