更改所选列表框项目的背景颜色 [英] Change background color for selected ListBox item

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

问题描述

到目前为止,这是我的 XAML.

This is my XAML so far.

<ScrollViewer Grid.Column="1" Grid.RowSpan="2">

    <ListBox   Background="Black" ItemsSource="{Binding Path=ActiveLog}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="Black">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition></RowDefinition>
                        <RowDefinition></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Column="0" Grid.Row="0" Foreground="White">
                        <TextBlock >Date:</TextBlock>
                        <TextBlock  Text="{Binding Path=LogDate}"/>
                    </TextBlock>
                    <TextBlock Grid.Column="1" Grid.Row="0" Foreground="White">
                        <TextBlock >Severity:</TextBlock>
                        <TextBlock  Text="{Binding Path=Severity}"/>
                    </TextBlock>
                    <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Foreground="LightGray" Text="{Binding Path=Message}"></TextBlock>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.Template>
            <ControlTemplate>
                <StackPanel Background="Black" IsItemsHost="True" >
                </StackPanel>
            </ControlTemplate>
        </ListBox.Template>

    </ListBox>
</ScrollViewer>

唯一的问题是所选项目的右侧有一个蓝色框.我假设有一种方法可以更改选择颜色,但我找不到.

The only problem is that the selected item has a blue box to the right. I assume there is a way to change the selection color, but I can't find it.

推荐答案

您需要使用 ListBox.ItemContainerStyle.

ListBox.ItemTemplate 指定应如何显示项目的内容.但是 WPF 仍然将每个项目包装在一个 ListBoxItem 控件中,默认情况下,如果它被选中,它的背景设置为系统突出显示颜色.您无法阻止 WPF 创建 ListBoxItem 控件,但您可以设置它们的样式——在您的情况下,将背景设置为始终为透明或黑色或其他——为此,您可以使用 ItemContainerStyle.

ListBox.ItemTemplate specifies how the content of an item should be displayed. But WPF still wraps each item in a ListBoxItem control, which by default gets its Background set to the system highlight colour if it is selected. You can't stop WPF creating the ListBoxItem controls, but you can style them -- in your case, to set the Background to always be Transparent or Black or whatever -- and to do so, you use ItemContainerStyle.

juFo 的回答显示了一种可能实现,通过劫持"item style上下文中的系统后台刷资源;另一种可能更惯用的技术是为 Background 属性使用 Setter.

juFo's answer shows one possible implementation, by "hijacking" the system background brush resource within the context of the item style; another, perhaps more idiomatic technique is to use a Setter for the Background property.

这篇关于更改所选列表框项目的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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