且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF:触发了ListBoxItem.IsSelected不工作的背景属性

更新时间:2022-10-21 13:53:00

体现出对航空式的一点点为我们提​​供了一个解释,为什么这个简单的触发设置不起作用。

在ListBoxItem中有一个触发器的控件模板需要precedence在我们的触发器。至少这似乎是一个MultiTrigger真。
从来就设法覆盖选=真正简单的触发器,但对于multitrigger我不得不做出我自己的ControlTemplate。

这是从Aero的风格,显示有问题的MultiTrigger模板:

 <的ControlTemplate的TargetType ={X:输入一个ListBoxItem}>
    < ControlTemplate.Triggers>
        <触发属性=IsSelectedVALUE =真正的>
            <二传手的TargetName =BDVALUE ={DynamicResource {X:静态HighlightBrush}}属性=背景/>
            <二传手值={DynamicResource {X:静态HighlightTextBrush}}属性=前景/>
        < /触发>
        < MultiTrigger>
            < MultiTrigger.Conditions>
                <条件属性=IsSelectedVALUE =真/>
                <条件属性=IsSelectionActiveVALUE =FALSE/>
            < /MultiTrigger.Conditions>
            <二传手的TargetName =BDVALUE ={DynamicResource {X:静态ControlBrush}}属性=背景/>
            <二传手值={DynamicResource {X:静态ControlTextBrush}}属性=前景/>
        < / MultiTrigger>
        <触发属性=IsEnabledVALUE =false的>
            <二传手值={DynamicResource {X:静态GrayTextBrush}}属性=前景/>
        < /触发>
    < /ControlTemplate.Triggers>
    < BORDER NAME =BD背景={TemplateBinding背景}BorderBrush ={TemplateBinding BorderBrush}了borderThickness ={TemplateBinding了borderThickness}填充={TemplateBinding填充}SnapsToDevicePixels =真>
        <内容presenter的Horizo​​ntalAlignment ={TemplateBinding Horizo​​ntalContentAlignment}VerticalAlignment ={TemplateBinding VerticalContentAlignment}SnapsToDevicePixels ={TemplateBinding SnapsToDevicePixels}/>
    < /边框>
< /控件模板>

希望它清除的东西一点点。我无法捉摸为什么they've过于复杂的款式这么多。

I try to change the Background property for my ListBoxItems using triggers in the ItemContainerStyle of my ListBox as follows:

    <ListBox Height="100" HorizontalAlignment="Left" Margin="107,59,0,0" Name="listBox1" VerticalAlignment="Top" Width="239">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="Lightblue"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Background" Value="Red"/>
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Background" Value="Yellow"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.Items>
            <ListBoxItem Content="First Item"/>
            <ListBoxItem Content="SecondItem"/>
            <ListBoxItem Content="Third Item"/>
        </ListBox.Items>
    </ListBox>

I would expect unselected items to have a light blue background, hovered items (i.e. when the mouse cursor is over them) to be yellow and selected items to be red.

For the unselected and hovered items this is working as expected, but the selected items still have their standard background color (i.e. blue, if the listbox has focus and light gray otherwise).

Is there anything I'm missing? Is this behaviour documented somewhere?

Thanks for any hint!

EDIT

I'm aware of the solution of overriding the default system colors (as described in WPF Listbox: Change selected and unfocused style to not be grayed out, thanks anyway for everyone posting this as an answer). However this is not what I want to do. I'm more interested in why my solution doesn't work.

I'm suspecting the standard ControlTemplate of ListItem to define it's own triggers which seem to take precendence over triggers defined by the style (perhaps someone could confirm this and point me to some resource where this behaviour is defined).

My solution for the meantime is to define a ControlTemplate for my ListItems like:

        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <Border Name="Border" Padding="2" SnapsToDevicePixels="true" Background="LightBlue" Margin="0">
                                <ContentPresenter/>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="true">
                                    <Setter TargetName="Border" Property="Background" Value="Red"/>
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListBox.ItemContainerStyle>

A little bit of reflecting on the Aero-style offers us an explanation to why this simple trigger-setting doesn't work.

The ListBoxItem has a ControlTemplate with triggers that takes precedence over our trigger. At least this seems to be true for a MultiTrigger. I´ve managed to override the simple trigger of Selected=true but for the multitrigger I had to make my own ControlTemplate.

This is the template from the Aero style that shows the problematic MultiTrigger:

<ControlTemplate TargetType="{x:Type ListBoxItem}">
    <ControlTemplate.Triggers>
        <Trigger Property="IsSelected" Value="true">
            <Setter TargetName="Bd" Value="{DynamicResource {x:Static HighlightBrush}}" Property="Background" />
            <Setter Value="{DynamicResource {x:Static HighlightTextBrush}}" Property="Foreground" />
        </Trigger>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="true" />
                <Condition Property="IsSelectionActive" Value="false" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd" Value="{DynamicResource {x:Static ControlBrush}}" Property="Background" />
            <Setter Value="{DynamicResource {x:Static ControlTextBrush}}" Property="Foreground" />
        </MultiTrigger>
        <Trigger Property="IsEnabled" Value="false">
            <Setter Value="{DynamicResource {x:Static GrayTextBrush}}" Property="Foreground" />
        </Trigger>
    </ControlTemplate.Triggers>
    <Border Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
    </Border>
</ControlTemplate>

Hope it clears things up a little bit. I can't fathom why they´ve overcomplicated the style this much.