当鼠标悬停(或选择)特定项目时,Datatrigger listview 的 ItemContainer 不起作用
Datatrigger listview's ItemContainer does not work when mouse is over (or selected) the specific item
在 ListView 中,我使用 DataTrigger 条件将项目(文本块)的背景颜色绑定到整数 属性,因为当 属性 更改(从 0 到 1)时,我想要项目的背景从透明变为红色。
此触发器在两种情况下不起作用:当鼠标悬停在项目上时,或者
选择此特定项目时。
在这两种情况下,背景总是透明的,尽管 属性 的值。
所以只有当鼠标悬停在其他不同的项目上,或者其他项目被选中时,项目才会改变它的背景。
textblock是在一个单独的DataTemplate中定义的(它是BoardViewModel的一部分),ListView是这些VM的集合。
在我的代码中,我检查了文本块的描述中没有 "isMouseOver" 活动触发条件,并且列表视图部分中没有 "isSelected" 活动触发器。
我也试过删除结构的背景,但对问题没有影响。
--数据模板---
<DataTemplate DataType="{x:Type viewModels:BoardViewModel}">
<Grid >
<Grid HorizontalAlignment="Stretch" Grid.Row="0" Width="Auto" Height="Auto">
<Border Margin="20,20,20,20" BorderThickness="2" BorderBrush="Transparent" HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Stretch">
<TextBlock.Text>
<MultiBinding StringFormat="ID: {0}">
<Binding Path="ID"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Border>
</Grid>
</Grid>
</DataTemplate>
--- 查看 ---
<Grid Grid.Row="0">
<ListView Height="Auto" Background="{StaticResource BoardBackground}" ItemsSource="{Binding Path= BoardViewModels}" SelectedItem="{Binding CurrentBoardViewModel}" IsSynchronizedWithCurrentItem="true">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="1">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="0">
<Setter Property="Background" Value="Transparent"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
I checked the correct value of the integer 属性 of the viewmodel, so I expected that item's background remains red also when item is selected or mouse is over the item, but something invalidate this assertion and I do不明白什么。
谢谢大家
尝试为 ListViewItem
定义自定义 ControlTemplate
,并将触发器添加到 <ControlTemplate.Triggers>
:
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/>
<SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/>
</Style.Resources>
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="1">
<Setter Property="Background" TargetName="Bd" Value="Red"></Setter>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
默认模板中的触发器优先于您的触发器。
在 ListView 中,我使用 DataTrigger 条件将项目(文本块)的背景颜色绑定到整数 属性,因为当 属性 更改(从 0 到 1)时,我想要项目的背景从透明变为红色。
此触发器在两种情况下不起作用:当鼠标悬停在项目上时,或者 选择此特定项目时。 在这两种情况下,背景总是透明的,尽管 属性 的值。
所以只有当鼠标悬停在其他不同的项目上,或者其他项目被选中时,项目才会改变它的背景。
textblock是在一个单独的DataTemplate中定义的(它是BoardViewModel的一部分),ListView是这些VM的集合。 在我的代码中,我检查了文本块的描述中没有 "isMouseOver" 活动触发条件,并且列表视图部分中没有 "isSelected" 活动触发器。 我也试过删除结构的背景,但对问题没有影响。
--数据模板---
<DataTemplate DataType="{x:Type viewModels:BoardViewModel}">
<Grid >
<Grid HorizontalAlignment="Stretch" Grid.Row="0" Width="Auto" Height="Auto">
<Border Margin="20,20,20,20" BorderThickness="2" BorderBrush="Transparent" HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Stretch">
<TextBlock.Text>
<MultiBinding StringFormat="ID: {0}">
<Binding Path="ID"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Border>
</Grid>
</Grid>
</DataTemplate>
--- 查看 ---
<Grid Grid.Row="0">
<ListView Height="Auto" Background="{StaticResource BoardBackground}" ItemsSource="{Binding Path= BoardViewModels}" SelectedItem="{Binding CurrentBoardViewModel}" IsSynchronizedWithCurrentItem="true">
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="1">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="0">
<Setter Property="Background" Value="Transparent"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</Grid>
I checked the correct value of the integer 属性 of the viewmodel, so I expected that item's background remains red also when item is selected or mouse is over the item, but something invalidate this assertion and I do不明白什么。 谢谢大家
尝试为 ListViewItem
定义自定义 ControlTemplate
,并将触发器添加到 <ControlTemplate.Triggers>
:
<Style TargetType="{x:Type ListViewItem}">
<Style.Resources>
<SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/>
<SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/>
<SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/>
<SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/>
</Style.Resources>
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListViewItem}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="False"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Selector.IsSelectionActive" Value="True"/>
<Condition Property="IsSelected" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/>
<Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
<DataTrigger Binding="{Binding Path= ErrorRedFlagByBoard}" Value="1">
<Setter Property="Background" TargetName="Bd" Value="Red"></Setter>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
默认模板中的触发器优先于您的触发器。