Concatenated Text in TextBlock DataTrigger

2019-09-11 04:40发布

问题:

I try to work with a concatenated text in the setter of a textblock.

textblock looks like that:

<TextBlock>
    <Run Text="{x:Static languages:visuTexts.Lenght}" />
    <Run Text="A [LA]"/>
    <TextBlock.Style>
        <Style TargetType="TextBlock" BasedOn="{StaticResource {x:Type TextBlock}}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Product.DiameterA}" Value="0">
                    <Setter Property="Text" Value="concatenated text here" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

i want to change Length A [LA] to Length [L] depending on the value of Produkt.DiameterA

回答1:

Use a Style for the Run element:

<TextBlock>
    <Run Text="{x:Static languages:visuTexts.Lenght}" />
    <Run>
        <Run.Style>
            <Style TargetType="Run">
                <Setter Property="Text" Value="A [LA]" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Product.DiameterA}" Value="0">
                            <Setter Property="Text" Value="[L]" />
                        </DataTrigger>
                    </Style.Triggers>
            </Style>
        </Run.Style>
    </Run>
</TextBlock>