WPF - 触发器问题

时间:2010-12-02 08:39:35

标签: wpf triggers styles

我有两个带有触发器的ListView,在选中时将背景颜色更改为深灰色,将前景颜色更改为白色。 问题是,当我在第一个列表视图中选择一个项目,然后在第二个列表视图中选择一个项目时,第一个列表视图前景中的项目不会再次变黑并保持白色。

alt text

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>

1 个答案:

答案 0 :(得分:0)

您的模板中的两个触发器之间会受到干扰。首次选择ListView#1中的值时,第一个IsSelected触发器变为活动状态。这会将TemplateBinding中“Content”的TextBlock.Foreground值覆盖为固定值White。

当ListView#1失去焦点到ListView#2时,第二个触发器(IsSelected和IsSelectionActive的MultiTrigger)也被激活。这会导致“Bd”的背景设置为不同的值(与其他触发器相同),并且因为它稍后在触发器集合中声明,所以会覆盖仍处于活动状态的前一个触发器。

Foreground setter也应该这样,但MultiTrigger中的一个是在父控件上而不是在“Content”上设置Foreground。由于“Content”不再使用TemplateBinding来提取父控件的Foreground值,因此第一个Trigger的White值在“Content”元素上保持活动状态。