开发者

WPF - problem with triggers

开发者 https://www.devze.com 2023-01-28 03:47 出处:网络
I have two ListViews with trigger that on selected change the background color to dark gray and the foreground color to white.

I have two ListViews with trigger that on selected change the background color to dark gray and the foreground color to white. The problem is that when I select an item in the first listview and then an item in the second listview the item in the first listview foreground doesn't get black again and it stay white.

WPF - problem with triggers

the xaml:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="190*" />
        <RowDefinition Height="121*" />
    </Grid.RowDefinitions>
    <Grid.Resources>
        <ResourceDictionary>
            <Style x:Key="@ListViewItemStyle" TargetType="{x:Type ListViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType='{x:Type ListViewItem}'>
                            <Grid SnapsToDevicePixels="True" Margin="0">
                                <Border x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}" />
                                <GridViewRowPresenter x:Name="Content" TextBlock.Foreground="{TemplateBinding Foreground}"
                        Content="{TemplateBinding Content}" Columns="{TemplateBinding GridView.ColumnCollection}" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter Property="TextElement.Foreground" Value="White" TargetName="Content" />
                                    <Setter Property="Background" Value="DarkGray" TargetName="Bd"/>
                                </Trigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="IsSelected" Value="true" />
                                        <Condition Property="Selector.IsSelectionActive" Value="false" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Background" TargetName="Bd"
                            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" />
                                </MultiTrigger>
                                <Trigger Property="IsEnabled" Value="false">
                                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <DataTemplate x:Key="@TextCellTemplate">
                <TextBlock Text="{Binding Name}"/>
            </DataTemplate>

            <DataTemplate x:Key="@TrubleCellTemplate">
                <Rectangle Width="20" Height="20" Fill="Black"></Rectangle>
            </DataTemplate>

        </ResourceDictionary>
    </Grid.Resources>


    <ListView ItemsSource="{Binding Persons}" Style="{DynamicResource @ListView}" ItemContainerStyle="{DynamicResource @ListViewItemStyle}">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="40" CellTemplate="{DynamicResource @TextCellTemplate}" />
                <GridViewColumn Width="131" CellTemplate="{DynamicResource 开发者_运维百科@TrubleCellTemplate}" />
            </GridView>
        </ListView.View>
    </ListView>

    <ListView ItemsSource="{Binding Persons}" Style="{DynamicResource @ListView}" ItemContainerStyle="{DynamicResource @ListViewItemStyle}" Grid.Row="1">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="40" CellTemplate="{DynamicResource @TextCellTemplate}" />
                <GridViewColumn Width="131" CellTemplate="{DynamicResource @TrubleCellTemplate}" />
            </GridView>
        </ListView.View>
    </ListView>

</Grid>


You're getting interference between two of the Triggers in your template. The first IsSelected Trigger becomes active when you first select the value in ListView #1. This overrides the TextBlock.Foreground value on "Content" from the TemplateBinding to a fixed value of White.

When ListView #1 loses focus to ListView #2 the second trigger (MultiTrigger for IsSelected and IsSelectionActive) is also activated. This causes the Background of "Bd" to be set to a different value (same as the other Trigger) and, because it's declared later in the Triggers collection, overrides the previous Trigger which is still active.

The same should happen for the Foreground setter, but the one in the MultiTrigger is setting the Foreground on the parent control instead of on "Content". Because "Content" is no longer using TemplateBinding to pull in the parent control's Foreground value the first Trigger's White value remains active on the "Content" element.

0

精彩评论

暂无评论...
验证码 换一张
取 消