TemplatedParent Binding in ControlTemplate.Triggers

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?

+3
source share
1 answer

TextBox? , , TextBlock , Trigger. TextBox , , (TextBlock). TexBox , Watermark . Watermark BackgroundText.

<ControlTemplate.Triggers>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsFocused" Value="false"/>
            <Condition Property="Text" Value="{x:Null}"/>
        </MultiTrigger.Conditions>
        <Setter TargetName="Watermark" Property="Visibility" Value="Visible"/>
    </MultiTrigger>
    <MultiTrigger>
        <MultiTrigger.Conditions>
            <Condition Property="IsFocused" Value="false"/>
            <Condition Property="Text" Value=""/>
        </MultiTrigger.Conditions>
        <Setter TargetName="Watermark" Property="Visibility" Value="Visible"/>
    </MultiTrigger>
</ControlTemplate.Triggers>
+1

Source: https://habr.com/ru/post/1728320/


All Articles