简体   繁体   English

WPF使用ItemsPanelTemplate样式选择列表框的选定项目

[英]WPF Style Selected Items of ListBox Using an ItemsPanelTemplate

How can I style the selected items in this listbox? 如何设置此列表框中所选项目的样式?

<ListBox x:Name="AssetTypeListBox" SelectionMode="Multiple">
    <ListBox.ItemsPanel >
        <ItemsPanelTemplate >
             <UniformGrid Columns="6"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel> 
</ListBox> 

I have tried 我努力了

<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#F15025"/>

and

<SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#F15025"/>

But they don't seem to affect the selected items as I would expect. 但是它们似乎并没有像我期望的那样影响所选项目。 Thanks. 谢谢。

If you just want to change the colour of the selection, then the easiest way is to set the solidcolorbrush as a resource on your Listbox: 如果只想更改选择的颜色,那么最简单的方法是将solidcolorbrush设置为列表框上的资源:

<ListBox x:Name="AssetTypeListBox" SelectionMode="Multiple">
    <ListBox.Resources>
         <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#F15025"/>
         <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}" Color="#F15025"/>
    </ListBox.Resources>
    <ListBox.ItemsPanel >
        <ItemsPanelTemplate >
             <UniformGrid Columns="6"/>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel> 
</ListBox> 

If you're wanting to customise the style of the selection of an item in the listbox, such as changing the selection colour or completely restyling the look of a selected item, you need to look at the ItemContainerStyle property. 如果要自定义列表框中项目的选择样式,例如更改选择颜色或完全重塑所选项目的外观,则需要查看ItemContainerStyle属性。

The ItemsPanelTemplate doesn't affect this - it simply sets the ItemsControl type that should layout and present the items - such as a StackPanel or UniformGrid. ItemsPanelTemplate不会对此产生影响-它只是设置应该布局和显示项目的ItemsControl类型-例如StackPanel或UniformGrid。

The basic hierarchy (with a pinch of salt) of a ListBox is: ListBox的基本层次结构(带有少许盐)是:

  • ListBox 列表框
    • ItemsPanelTemplate (StackPanel by default) ItemsPanelTemplate(默认为StackPanel)
      • ItemsContainerStyle (Selection highlighting is here) ItemsContainerStyle(选择突出显示在此处)
        • ItemTemplate (You define the content of your list item here) ItemTemplate(您在此处定义列表项的内容)

The differences between ItemTemplates and ItemsContainerStyle are covered here: What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox? 这里介绍了ItemTemplates和ItemsContainerStyle之间的区别: WPF ListBox中的ItemTemplate和ItemContainerStyle之间有什么区别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM