选中WPF的ListBoxItem背景并且Window失去焦点

时间:2016-02-01 23:08:57

标签: c# wpf listbox listboxitem mahapps.metro

当我遇到以下问题时,我正在使用MahApps:Metro(不知道它是否相关)开展研究项目:

如果选择了sleep <{1}}并且@mock.patch('time.sleep', lambda: None)失去焦点,是否有办法更改Background ListBoxItem

请参阅以下图片以说明:

Image number one

这里我们可以看到我的ListBox,当窗口 焦点时,选择了第一个项目。

Image number two

在第二张图片上,我们可以看到焦点位于Window二号时的样子。

我想知道是否有办法将Window的蓝色Background更改为LightGray,例如,当窗口失去焦点时,。< / p>

这是我到目前为止所看到的:

  • 覆盖SelectedItem(实际上很有效,但在窗口有焦点时替换样式)[1] [2] [3]
  • 覆盖ControlBrushKey(同一问题)[1]

谢谢!

2 个答案:

答案 0 :(得分:3)

下面的示例解决了您的问题。 请注意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>

Output

答案 1 :(得分:2)

目前只有在您自己覆盖ListBoxItem样式时才会出现这种情况(我将在下一版MahApp中对此进行更改)。

这就是你需要的:

<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}" />

希望这有帮助!