在ListView中,我使用DataTrigger条件将Item(文本块)的背景色绑定到Integer属性,因为当该属性从0更改为1时,我希望该项目的背景从透明变为红色。
此触发器在两种情况下不起作用:当鼠标悬停在该项目上时,或 选择此特定项目时。 在这两种情况下,尽管具有财产价值,但背景始终是透明的。
因此,仅当鼠标悬停在其他不同项目上或选择了其他项目时,该项目才会更改其背景。
该文本块在单独的DataTemplate中定义(它是BoardViewModel的一部分),而ListView是这些VM的集合。 在我的代码中,我检查了文本块描述中是否没有“ isMouseOver”活动触发器条件,并且列表视图的部分中没有“ isSelected”活动触发器。 我还尝试删除结构的背景,但这对问题没有影响。
-DataTemplate ---
<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>
我检查了viewmodel的integer属性的正确值,所以我希望当选择了项目或鼠标悬停在项目上时,项目的背景也会保持红色,但是有些东西使该断言无效并且我不明白。 谢谢大家。
答案 0 :(得分:1)
尝试为ControlTemplate
定义自定义ListViewItem
,然后将触发器添加到<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>
默认模板中的触发器优先于您的触发器。