1
votes

I have the following TextBlock Style:

    <Style TargetType="TextBlock" x:Key="MyValues">
    <Setter Property="FontStyle" Value="Italic"/>
    <Setter Property="Foreground" Value="DarkBlue"/>
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsMetric}" Value="True">
            <Setter Property="Text">
                <Setter.Value>
                    <MultiBinding StringFormat="F1">

                    </MultiBinding>
                </Setter.Value>
            </Setter>
            <Setter Property="Foreground" Value="Red"/>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsMetric}" Value="False">
            <Setter Property="Text">
                <Setter.Value>
                    <MultiBinding StringFormat="F3">

                    </MultiBinding>
                </Setter.Value>
            </Setter>
            <Setter Property="Foreground" Value="Green"/>
        </DataTrigger>
    </Style.Triggers>

I then use TextBlocks as follows:

<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=Breadth}" Style="{StaticResource MyValues}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=Depth}" Style="{StaticResource MyValues}"/>
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Path=Area}" Style="{StaticResource MyValues}" />

The intention is to set the StringFormat depending on a bound property IsMetric. The Binding in the style are left empty because i want to apply the same style for multiple TextBlocks all bound to different properties. The triggers are working but the StringFormat` is ignored, any ideas?

1
this would be a lot easier with a converter - thumbmunkeys

1 Answers

2
votes

Here you set Text property to be different things in TextBlock declaration and in DataTriggers. In the first case it's an instance of Binding class. In the second case it's an instance of MultiBinding class. Finally it is one of these. It cannot be both at the moment.

The following markup

<Setter Property="Text">
    <Setter.Value>
        <MultiBinding StringFormat="F1">

        </MultiBinding>
    </Setter.Value>
</Setter>

instantiates MultiBinding class instance and sets it to Text property.

The Text="{Binding Path=Breadth}" instantiates Binding class instance and sets it to Text property.