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

时间:2019-02-06 00:00:04

标签: wpf wpf-controls

如何设置此列表框中所选项目的样式?

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

我尝试过

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

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

但是它们似乎并没有像我期望的那样影响所选项目。谢谢。

1 个答案:

答案 0 :(得分:0)

如果只想更改所选内容的颜色,那么最简单的方法是将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> 

如果您要自定义列表框中项目的 选择 的样式,例如更改选择颜色或完全重塑所选对象的外观项,则需要查看ItemContainerStyle属性。

ItemsPanelTemplate不会对此产生影响-它只是设置应该布局和显示项目的ItemsControl类型,例如StackPanel或UniformGrid。

ListBox的基本层次结构(带有少许盐)是:

  • 列表框
    • ItemsPanelTemplate(默认为StackPanel)
      • ItemsContainerStyle(选择突出显示在此处)
        • ItemTemplate(您在此处定义列表项的内容)

这里涵盖了ItemTemplates和ItemsContainerStyle之间的区别: What's the difference between ItemTemplate and ItemContainerStyle in a WPF ListBox?

相关问题