选择 WPF 的 ListBoxItem 背景并 Window 失去焦点时更改它
Change WPF's ListBoxItem Background when it's selected and Window lost focus
当我遇到以下问题时,我正在使用 MahApps:Metro(不知道它是否相关)进行研究项目:
有没有办法改变 ListBoxItem
的 Background
只有 被选中并且 Window
失去焦点?
见下图说明:
这里我们可以看到我的列表框,当 window 有 焦点时,第一个项目被选中。
在第二张图片上,我们可以看到当焦点在 Window
二号上时的样子。
我想知道是否有办法将 SelectedItem
的蓝色 Background
更改为浅灰色,例如 only 当Window 失去焦点。
以下是我目前查看的内容:
- 覆盖
ControlBrushKey
(实际上是一种工作,但在 window 具有焦点时替换样式)[1] [2] [3]
- 覆盖
HighlightBrushKey
(同样的问题)[1]
谢谢!
目前只有您自己覆盖 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}" />
希望对您有所帮助!
下面的例子解决了你的问题。
注意 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>
当我遇到以下问题时,我正在使用 MahApps:Metro(不知道它是否相关)进行研究项目:
有没有办法改变 ListBoxItem
的 Background
只有 被选中并且 Window
失去焦点?
见下图说明:
这里我们可以看到我的列表框,当 window 有 焦点时,第一个项目被选中。
在第二张图片上,我们可以看到当焦点在 Window
二号上时的样子。
我想知道是否有办法将 SelectedItem
的蓝色 Background
更改为浅灰色,例如 only 当Window 失去焦点。
以下是我目前查看的内容:
- 覆盖
ControlBrushKey
(实际上是一种工作,但在 window 具有焦点时替换样式)[1] [2] [3] - 覆盖
HighlightBrushKey
(同样的问题)[1]
谢谢!
目前只有您自己覆盖 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}" />
希望对您有所帮助!
下面的例子解决了你的问题。
注意 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>