【问题标题】:WPF - problem with triggersWPF - 触发器问题
【发布时间】:2011-05-19 00:01:19
【问题描述】:

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

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>

【问题讨论】:

  • 只是预感.. 看看在 ResourceDictionary 中的 Style 定义(元素)中添加 x:Shared="false" 是否可以解决您的问题。
  • @Gishu - 谢谢,但没用
  • @Erez - 好的。尝试添加反向触发器,即当 IsSelected="False" 或在样式本身中指定默认状态时。当触发器不起作用时,它们可能会非常烦人 - 我最近遇到了一个 - 通读这篇文章,看看你是否有任何线索geekswithblogs.net/thibbard/archive/2008/05/13/…
  • 可能是因为第一个 ListView 中的那个项目仍然被选中?我不清楚您是否想在不再选择项目时恢复到原始样式,或者只是在项目失去焦点时(在这种情况下,您可能想用 IsFocused 替换 IsSelected)......也只是一种预感。
  • @Andrei Pana - 谢谢!!!它奏效了。

标签: wpf triggers styles


【解决方案1】:

您在模板中的两个触发器之间受到干扰。当您第一次选择 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”元素上保持活动状态。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 2011-08-06
    • 1970-01-01
    • 2011-03-17
    • 2014-10-02
    相关资源
    最近更新 更多