在 WPF 中绑定 ListBox 项目背景和选择颜色
Bind ListBox item background & selection colors in WPF
我有一个列表,其中每个项目的背景颜色应取决于两件事:selection 状态和数据上下文中的布尔值 属性。我已经想出如何绑定背景颜色,但是一旦我 select 列表项,我的自定义背景就会消失。
理想情况下,最终结果是(取决于 selection 状态)如果数据上下文布尔值为真则为绿色阴影,如果为假则为红色阴影。
MainWindow.xaml:
<ListBox x:Name="CrewList" ItemsSource="{Binding CrewList}" SelectedItem="{Binding SelectedCrew}" Style="{StaticResource EventOverviewListBox}" Grid.Column="1" Grid.Row="0">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Data.IsValid}" Value="true">
<Setter Property="Background" Value="{StaticResource StatusValid}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Data.IsValid}" Value="false">
<Setter Property="Background" Value="{StaticResource StatusInvalid}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="10,10,10,10" KeyboardNavigation.IsTabStop="False">
<TextBlock Text="{Binding Lane}" Margin="0,0,20,0" FontSize="20" />
<TextBlock Text="{Binding ClubName}" Foreground="Black" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
App.xaml:
<SolidColorBrush x:Key="StatusValid">LawnGreen</SolidColorBrush>
<SolidColorBrush x:Key="StatusInvalid">Red</SolidColorBrush>
<Style x:Key="EventOverviewListBox" TargetType="ListBox">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Background" Value="Teal"/>
</Style>
<Style x:Key="EventOverviewListBoxItem" TargetType="ListBoxItem">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>
我找了又找,但在过去的几个小时里什么也没找到,所以我希望其他人知道办法。
谢谢!
我假设 "selection state" 你的意思是该项目是否被选中。
ListBoxItem 有一个 ControlTemplate,其触发器优先于触发器。您需要为 ListBoxItem 样式创建自己的 ControlTemplate。
另外,使用 MultiDataTrigger.
像这样:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="BorderWrap">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}" Value="True" />
<Condition Binding="{Binding Data.IsValid}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="BorderWrap" Property="Background" Value="{StaticResource StatusValid}"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}" Value="True" />
<Condition Binding="{Binding Data.IsValid}" Value="False" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="BorderWrap" Property="Background" Value="{StaticResource StatusInvalid}"/>
<MultiDataTrigger.Setters>
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我有一个列表,其中每个项目的背景颜色应取决于两件事:selection 状态和数据上下文中的布尔值 属性。我已经想出如何绑定背景颜色,但是一旦我 select 列表项,我的自定义背景就会消失。
理想情况下,最终结果是(取决于 selection 状态)如果数据上下文布尔值为真则为绿色阴影,如果为假则为红色阴影。
MainWindow.xaml:
<ListBox x:Name="CrewList" ItemsSource="{Binding CrewList}" SelectedItem="{Binding SelectedCrew}" Style="{StaticResource EventOverviewListBox}" Grid.Column="1" Grid.Row="0">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding Data.IsValid}" Value="true">
<Setter Property="Background" Value="{StaticResource StatusValid}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Data.IsValid}" Value="false">
<Setter Property="Background" Value="{StaticResource StatusInvalid}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="10,10,10,10" KeyboardNavigation.IsTabStop="False">
<TextBlock Text="{Binding Lane}" Margin="0,0,20,0" FontSize="20" />
<TextBlock Text="{Binding ClubName}" Foreground="Black" VerticalAlignment="Center"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
App.xaml:
<SolidColorBrush x:Key="StatusValid">LawnGreen</SolidColorBrush>
<SolidColorBrush x:Key="StatusInvalid">Red</SolidColorBrush>
<Style x:Key="EventOverviewListBox" TargetType="ListBox">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="Background" Value="Teal"/>
</Style>
<Style x:Key="EventOverviewListBoxItem" TargetType="ListBoxItem">
<Setter Property="KeyboardNavigation.IsTabStop" Value="False"/>
</Style>
我找了又找,但在过去的几个小时里什么也没找到,所以我希望其他人知道办法。
谢谢!
我假设 "selection state" 你的意思是该项目是否被选中。
ListBoxItem 有一个 ControlTemplate,其触发器优先于触发器。您需要为 ListBoxItem 样式创建自己的 ControlTemplate。 另外,使用 MultiDataTrigger.
像这样:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border Name="BorderWrap">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}" Value="True" />
<Condition Binding="{Binding Data.IsValid}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="BorderWrap" Property="Background" Value="{StaticResource StatusValid}"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding RelativeSource={RelativeSource Self},Path=IsSelected}" Value="True" />
<Condition Binding="{Binding Data.IsValid}" Value="False" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter TargetName="BorderWrap" Property="Background" Value="{StaticResource StatusInvalid}"/>
<MultiDataTrigger.Setters>
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>