设置背景颜色在ListBox选定项 [英] Set background color for selected items in a ListBox

查看:176
本文介绍了设置背景颜色在ListBox选定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法为在列表框中选定项的背景色。我不希望在本实施例的交变的颜色。我把他们作为一个试验和他们的工作。触发IsSelected被解雇的粗细去大胆的前景变为红色。高亮颜色画笔设置为钢青,因为它消失时,ListBox中失去焦点并没有达到预期的效果。当列表框失去焦点,是我想要的红色粗体并持有。我想要的背景颜色,持有所选项目。现在所选项目的背景是白色的,当ListBox中失去焦点持有。感谢您的帮助,我会测试任何建议修复。

 <列表框Grid.Row =1Grid.Column =0Grid.ColumnSpan =1NAME =WFEnum能见度=可见了borderThickness =2 保证金=1填充=2,2,7,2
             的ItemsSource ={绑定路径= SearchItem.SrchWorkFlows}的Horizo​​ntalAlignment =左
             presentationTraceSources.TraceLevel =高AlternationCount =2>
        < ListBox.ItemContainerStyle>
            <风格的TargetType =ListBoxItem的>
                < setter属性=VerticalContentAlignmentVALUE =中心/>
                < Style.Triggers>
                    <触发属性=ItemsControl.AlternationIndexVALUE =0>
                        < setter属性=背景VALUE =浅绿>< /二传手>
                    < /触发>
                    <触发属性=ItemsControl.AlternationIndexVALUE =1>
                        < setter属性=背景VALUE =浅粉红>< /二传手>
                    < /触发>
                    <触发属性=IsSelectedVALUE =真>
                        < setter属性=粗细VALUE =大胆/>
                        < setter属性=背景VALUE =钢青/>
                        < setter属性=前景VALUE =红/>
                    < /触发>
                < /Style.Triggers>
                < Style.Resources>
                    <的SolidColorBrush X:键={X:静态SystemColors.HighlightBrushKey}颜色=透明/>
                < /Style.Resources>
            < /样式和GT;
        < /ListBox.ItemContainerStyle>
        < ListBox.ItemTemplate>
            <&DataTemplate的GT;
                < TextBlock的文本={绑定路径=名称,模式=单向}背景=透明/>
            < / DataTemplate中>
        < /ListBox.ItemTemplate>
    < /列表框>


解决方案

您指定一个的ListBox 与SystemColors.HighlightBrushKey(聚焦)和SystemColors.ControlBrushKey则selectedItem背景(不集中)

 < Style.Resources>
    <! - 聚焦时所选项目的背景 - >
    <的SolidColorBrush X:键={X:静态SystemColors.HighlightBrushKey}
                     颜色=绿色/>
    <! - 当不集中所选项目的背景 - >
    <的SolidColorBrush X:键={X:静态SystemColors.ControlBrushKey}
                     颜色=浅绿/>
< /Style.Resources>

I cannot set the background color for the selected item on a list box. I don't want the alternating colors in this example. I put them in as a test and they work. Trigger IsSelected is firing as the fontweight goes to bold and the foreground goes to red. Setting highlight color brush to SteelBlue does not achieve the desired effect as it goes away when the ListBox loses focus. Red and bold does hold when the ListBox loses focus and is what I want. I want the background color to take and hold for the selected item. Right now the background for the selected items is white and holds when the ListBox loses focus. Thanks for your help and I will test any proposed fix.

    <ListBox Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="1" Name="WFEnum" Visibility="Visible" BorderThickness="2" Margin="1" Padding="2,2,7,2"
             ItemsSource="{Binding Path=SearchItem.SrchWorkFlows}" HorizontalAlignment="Left" 
             PresentationTraceSources.TraceLevel="High" AlternationCount="2" >
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="VerticalContentAlignment" Value="Center" />
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                        <Setter Property="Background" Value="LightGreen"></Setter>
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                        <Setter Property="Background" Value="LightPink"></Setter>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True" >
                        <Setter Property="FontWeight" Value="Bold" />
                        <Setter Property="Background" Value="SteelBlue" />
                        <Setter Property="Foreground" Value="Red" />
                    </Trigger>
                </Style.Triggers>
                <Style.Resources>
                    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/>
                </Style.Resources>
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=Name, Mode=OneWay}" Background="Transparent" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

解决方案

You specify the SelectedItem Background for a ListBox with the SystemColors.HighlightBrushKey (focused) and SystemColors.ControlBrushKey (not focused)

<Style.Resources>
    <!-- Background of selected item when focussed -->
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                     Color="Green"/>
    <!-- Background of selected item when not focussed -->
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}"
                     Color="LightGreen" />
</Style.Resources> 

这篇关于设置背景颜色在ListBox选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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