【问题标题】:Change WPF's ListBoxItem Background when it's selected and Window lost focus选择 WPF 时更改 WPF 的 ListBoxItem 背景并且窗口失去焦点
【发布时间】:2016-02-01 23:08:57
【问题描述】:
当我遇到以下问题时,我正在使用MahApps:Metro(不知道是否相关)进行研究项目:
当ListBoxItem 被选中并且Window 失去焦点时,有没有办法改变ListBoxItem 的Background仅?
请看以下图片来说明:
在这里我们可以看到当窗口具有焦点时选择的第一个项目的列表框。
在第二张图片上,我们可以看到焦点位于第二个 Window 时的样子。
我想知道是否有办法将 SelectedItem 的蓝色 Background 更改为 LightGray,例如,仅当窗口失去焦点时。
这是我目前看到的:
- 覆盖
ControlBrushKey(其实有点用,但是当窗口有焦点时替换样式)[1][2][3]
- 覆盖
HighlightBrushKey(同样的问题)[1]
谢谢!
【问题讨论】:
标签:
c#
wpf
listbox
listboxitem
mahapps.metro
【解决方案1】:
以下示例解决了您的问题。
注意MultiDataTrigger。
<Window.Resources>
<DataTemplate x:Key="DataTemplate1">
<Grid Width="200" Background="Lime">
<TextBlock Text="{Binding}" Foreground="Black"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DataTemplate2">
<Grid Width="200" Background="DarkGray">
<TextBlock Text="{Binding}" Foreground="Black"/>
</Grid>
</DataTemplate>
<DataTemplate x:Key="DataTemplate1Sel">
<Grid Width="200" Background="Coral">
<TextBlock Text="{Binding}" Foreground="Black"/>
</Grid>
</DataTemplate>
</Window.Resources>
<ListBox x:Name="Lst" Margin="0,56,10,0">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="ContentTemplate" Value="{DynamicResource DataTemplate1Sel}"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter Property="ContentTemplate" Value="{DynamicResource DataTemplate1}"/>
</Trigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding IsActive, RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}}" Value="False"/>
<Condition Binding="{Binding IsSelected, RelativeSource={RelativeSource Self}}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="ContentTemplate" Value="{DynamicResource DataTemplate2}"/>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</ListBox.Resources>
</ListBox>
【解决方案2】:
目前只有当您自己覆盖 ListBoxItem 样式时才有可能(我将在下一个版本的 MahApps 中更改此设置)。
这是你需要的:
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource GrayBrush7}" />
</MultiTrigger>
完整样式:
<Style x:Key="CustomMetroListBoxItem"
BasedOn="{StaticResource MetroListBoxItem}"
TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Border"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<ContentPresenter Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{DynamicResource AccentColorBrush}" />
<Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}" />
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{DynamicResource AccentColorBrush3}" />
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource GrayBrush7}" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsEnabled" Value="False" />
<Condition Property="IsSelected" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource GrayBrush7}" />
<Setter Property="Foreground" Value="{DynamicResource AccentSelectedColorBrush}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="False" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource GrayBrush7}" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="True" />
<Condition Property="Selector.IsSelectionActive" Value="True" />
</MultiTrigger.Conditions>
<Setter Property="Background" Value="{DynamicResource AccentColorBrush2}" />
</MultiTrigger>
</Style.Triggers>
</Style>
用法:
<ListBox ItemContainerStyle="{StaticResource CustomMetroListBoxItem}"
Style="{StaticResource VirtualisedMetroListBox}" />
希望这会有所帮助!