WPF:触发了ListBoxItem.IsSelected不工作的背景属性 [英] WPF: Trigger for ListBoxItem.IsSelected not working for Background property

查看:1009
本文介绍了WPF:触发了ListBoxItem.IsSelected不工作的背景属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变背景属性我使用触发器 ListBoxItem的 S中的 ItemContainerStyle 我的的ListBox 如下:

 <列表框高度=100的Horizo​​ntalAlignment =左保证金=107,59,0,0NAME =listBox1中VerticalAlignment =评出的WIDTH =239 >
        < ListBox.ItemContainerStyle>
            <风格的TargetType ={X:输入一个ListBoxItem}>
                < setter属性=背景VALUE =Lightblue/>
                < Style.Triggers>
                    <触发属性=IsSelectedVALUE =真正的>
                        < setter属性=背景VALUE =红/>
                    < /触发>
                    <触发属性=IsMouseOverVALUE =真正的>
                        < setter属性=背景值=黄色/>
                    < /触发>
                < /Style.Triggers>
            < /样式和GT;
        < /ListBox.ItemContainerStyle>
        < ListBox.Items>
            < ListBoxItem的内容=第一项/>
            < ListBoxItem的内容=SecondItem/>
            < ListBoxItem的内容=第三项/>
        < /ListBox.Items>
    < /列表框>

我希望未选定项目有一个淡蓝色背景,盘旋的项目(即当鼠标光标在他们)是黄色的和选择的项目是红色的。

有关的选择,而这种徘徊按预期方式工作项目,但所选的项目仍然有其标准的背景颜色(即蓝,如果列表框具有焦点和浅灰色其他)。

有什么我失踪?这种行为记录的地方?

感谢您的任何暗示!

修改

我知道重写系统默认的颜色(如<所描述的解决方案href=\"http://stackoverflow.com/questions/698830/wpf-listbox-change-selected-and-unfocused-style-to-not-be-grayed-out\">WPF列表框:更改选择而散的风格不被反正每个人都张贴这作为一个答案)变灰,谢谢。然而,这不是我想做的事情。我更感兴趣的是,为什么我的解决方案是行不通的。

我怀疑列表项标准控件模板来定义它自己的触发器,这似乎采取precendence在由样式定义触发器(也许有人可以证实这一点,并指出我的一些资源,其中这种行为定义)。

我对其间的解决方案是定义一个控件模板我的列表项就像:

 &LT; ListBox.ItemContainerStyle&GT;
            &LT;风格的TargetType ={X:输入一个ListBoxItem}&GT;
                &LT; setter属性=模板&GT;
                    &LT; Setter.Value&GT;
                        &LT;的ControlTemplate的TargetType =ListBoxItem的&GT;
                            &LT; BORDER NAME =边框填充=2SnapsToDevicePixels =真正的背景=LightBlue保证金=0&GT;
                                &lt;内容presenter /&GT;
                            &LT; /边框&GT;
                            &LT; ControlTemplate.Triggers&GT;
                                &LT;触发属性=IsSelectedVALUE =真正的&GT;
                                    &LT;二传手的TargetName =边框属性=背景VALUE =红/&GT;
                                &LT; /触发&GT;
                            &LT; /ControlTemplate.Triggers>
                        &LT; /控件模板&GT;
                    &LT; /Setter.Value>
                &LT; /二传手&GT;
            &LT; /样式和GT;
        &LT; /ListBox.ItemContainerStyle>


解决方案

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

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

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

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

希望它清除的东西一点点。我无法捉摸为什么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.

这篇关于WPF:触发了ListBoxItem.IsSelected不工作的背景属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆