简体   繁体   English

当WPF的ListBoxItem背景被选中并且Window失去焦点时,将其更改

[英]Change WPF's ListBoxItem Background when it's selected and Window lost focus

I was working on a Study project using MahApps:Metro (don't know if it's relevant) when I got stuck in the following problem: 当我遇到以下问题时,我正在使用MahApps:Metro进行研究项目(不知道它是否相关):

Is there a way of change the Background of a ListBoxItem only when it's selected and the Window loses focus? 当选择了ListBoxItemWindow失去焦点时, 可以更改它的Background吗?

See the following images to ilustrate: 请参见以下图像进行说明:

图片第一

Here we can see my ListBox with the first item selected when the window has focus. 在这里,我们可以看到我的列表框与在窗口集中选择的第一个项目。

图片二

And on this second image we can see how it looks like when the focus is on the Window number two. 在第二张图像上,我们可以看到焦点在第二个Window上时的样子。

I would like to know if there's a way of changing the Blue Background of the SelectedItem to a LightGray, for example, only when the Window loses focus. 我想知道是否有一种方法,例如, 当Window失去焦点时, 才可以SelectedItem的蓝色Background更改为浅灰色。

Here's what I've looked so far: 到目前为止,这是我的外观:

  • Overwrite ControlBrushKey (actually kind of works, but replaces the style when the window has focus) [1] [2] [3] 覆盖ControlBrushKey (实际上是一种作品,但是当窗口具有焦点时会替换样式) [1] [2] [3]
  • Overwrite HighlightBrushKey (same problem) [1] 覆盖HighlightBrushKey (相同的问题) [1]

Thank you! 谢谢!

Below example solves your problem. 下面的示例解决了您的问题。 Note the MultiDataTrigger . 注意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>

输出量

This is currently only possible if you override the ListBoxItem style by yourself (I'll change this in the next releases of MahApps). 当前只有在您自己覆盖ListBoxItem样式的情况下才有可能(我将在下一版MahApps中对此进行更改)。

This is what you need: 这是您需要的:

<MultiTrigger>
    <MultiTrigger.Conditions>
        <Condition Property="IsSelected" Value="True" />
        <Condition Property="Selector.IsSelectionActive" Value="False" />
    </MultiTrigger.Conditions>
    <Setter Property="Background" Value="{DynamicResource GrayBrush7}" />
</MultiTrigger>

Complete style: 完整款式:

<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>

Usage: 用法:

<ListBox ItemContainerStyle="{StaticResource CustomMetroListBoxItem}"
         Style="{StaticResource VirtualisedMetroListBox}" />

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM