1

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
  • 1
    this would be a lot easier with a converter Commented Apr 1, 2012 at 13:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Makes sense! Is there a way round this?
Can't see any way around other than the one proposed by @pivotnig - using ValueConverter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.