WPF-触发器问题
我有两个带有触发器的列表视图,在选中时将背景颜色更改为深灰色,将前景颜色更改为白色。 问题是,当我在第一个listview中选择一个项目,然后在第二个listview中选择一个项目时,第一个listview前景中的项目不会再次变为黑色,而是保持为白色 xaml:WPF-触发器问题,wpf,triggers,styles,Wpf,Triggers,Styles,我有两个带有触发器的列表视图,在选中时将背景颜色更改为深灰色,将前景颜色更改为白色。 问题是,当我在第一个listview中选择一个项目,然后在第二个listview中选择一个项目时,第一个listview前景中的项目不会再次变为黑色,而是保持为白色 xaml: <Grid> <Grid.RowDefinitions> <RowDefinition Height="190*" /> <RowDefinition H
<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>
模板中的两个触发器之间存在干扰。在ListView 1中首次选择值时,第一个IsSelected触发器将激活。这会将TemplateBinding中内容的TextBlock.前台值覆盖为白色的固定值 当ListView 1与ListView 2失去焦点时,IsSelected和IsSelectionActive的第二个触发器MultiTrigger也会激活。这会导致Bd的背景设置为与另一个触发器相同的不同值,并且由于它稍后在触发器集合中声明,因此会覆盖仍处于活动状态的前一个触发器
前景设置器也应该如此,但MultiTrigger中的设置器是在父控件上而不是在内容上设置前景。因为内容不再使用TemplateBinding拉入父控件的前景值,所以第一个触发器的白色值在内容元素上保持活动状态。只是一种预感。。查看将x:Shared=false添加到ResourceDictionary中的样式定义元素是否可以解决您的问题。@Gishu-谢谢,但没有work@Erez-好的。尝试添加反向触发器,即IsSelected=False或在样式本身中指定默认状态。当触发器不起作用时会很烦人-我最近遇到了一个-通读这篇文章,看看你是否有任何线索,可能是因为第一个列表视图中的项目仍然处于选中状态?我不清楚,当项目不再被选中时,或者只是当项目失去焦点时,您是否希望恢复到原始样式,在这种情况下,您可能希望将IsSelected替换为IsFocused。。。也是一种预感。@AndreiPana-谢谢!!!成功了。