问题
Here is the xaml that I am working on:
<TextBlock Text="{Binding Title}" Margin="10,0,0,0" VerticalAlignment="Center">
<TextBlock.Resources>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Margin" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
</TextBlock.Resources>
</TextBlock>
When text = "" I want to clear up the margin. But somehow it does not work.
回答1:
You must move Margin="10,0,0,0"
from TextBlock
to setter of Style
:
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Margin" Value="10,0,0,0" />
<Style.Triggers>
<Trigger Property="Text" Value="">
<Setter Property="Margin" Value="0" />
</Trigger>
</Style.Triggers>
</Style>
Because local value has higher precedence
order over style setters and triggers:
Property system coercion.
Active animations, or animations with a Hold behavior.
3. Local value.
TemplatedParent template properties.
Implicit style.
6. Style triggers.
Template triggers.
8. Style setters.
...
For more information, please see:
MSDN: Dependency Property Value Precedence
来源:https://stackoverflow.com/questions/22371291/trigger-based-on-text-property-not-working