Trigger based on text property not working

江枫思渺然 提交于 2021-02-11 05:08:21

问题


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:

  1. Property system coercion.

  2. Active animations, or animations with a Hold behavior.

    3. Local value.

  3. TemplatedParent template properties.

  4. Implicit style.

    6. Style triggers.

  5. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!