When its text is empty, I am attempting to set the background of a custom control to a visual brush using a trigger in the ControlTemplate. The following shows the relevant code:
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="">
<Setter TargetName="MyBorder" Property="Background">
<Setter.Value>
<VisualBrush Opacity="0.4" Stretch="None" TileMode="None">
<VisualBrush.Visual>
<TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BackgroundText}" />
</VisualBrush.Visual>
</VisualBrush>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
When the text is empty, however, the visual brush is not applied. However, if I create the visual brush in code and expose it as a dependency property, the following code does work:
<ControlTemplate.Triggers>
<Trigger Property="Text" Value="">
<Setter TargetName="MyBorder" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=BackgroundBrush}" />
</Setter>
</Trigger>
</ControlTemplate.Triggers>
I would rather define the brush in XAML, though. Why does the second binding work correctly but not the first?