When its text is empty, I try to set the background of the custom control to a visual brush using a trigger in the ControlTemplate. The following code is shown:
<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>
However, when the text is empty, the visual brush is not applied. However, if I create a visual brush in the code and expose it as a dependency property, the following code really works:
<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 a brush in XAML. Why is the second binding working correctly, but not the first?
source
share