tags:

views:

72

answers:

2

Look at the line "THIS LINE ####" in following example.

<ListBox Grid.Row="0" x:Name="listBoxServers">
<ListBoxItem HorizontalContentAlignment="Stretch">
    <StackPanel>
        <TextBlock><Run Text="My computer"/></TextBlock>
        <TextBlock Foreground="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}">
            <TextBlock.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="True">
                            <Setter Property="TextBlock.Foreground" Value="White" /> <!-- THIS LINE #### How can I get this work? -->
                            <Setter Property="TextBlock.Background" Value="Blue" /> <!-- This line here for debugging purposes (to show that these really are called) -->
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </TextBlock.Style>
            <Run Text="localhost"/>
        </TextBlock>
    </StackPanel>
</ListBoxItem>
</ListBox>

How can I get the following trigger to override the value?

(Btw, example above is just compressed. (In real application the the Style is in its own resource.))

A: 

You should be able to do this with two data triggers, one for true, and one for false.

    <TextBlock>
        <TextBlock.Style>
            <Style>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="True">
                        <Setter Property="TextBlock.Foreground" Value="White" />
                        <Setter Property="TextBlock.Background" Value="Blue" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" Value="False">
                        <Setter Property="TextBlock.Foreground" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
        <Run Text="localhost"/>
    </TextBlock>
Reed Copsey
Oh, of course, great workaround... Wondering is there even need for overriding precedence? (No in this case but some other corner cases...)
Ciantic
I'm not sure there's a way to easily bind it plus override it, without doing something like my solution... I think you can either bind it, and change the bound property, or change using datatriggers as above...
Reed Copsey
A: 

This post might explain why the trigger doesn't fire when you have assigned a local value to the property.