【问题标题】:WPF Style Selected Items of ListBox Using an ItemsPanelTemplate使用 ItemsPanelTemplate 的 WPF 样式列表框的选定项
【发布时间】:2019-02-06 00:00:03
【问题描述】:

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

<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"/>

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

【问题讨论】:

    标签: wpf wpf-controls


    【解决方案1】:

    如果你只是想改变选择的颜色,那么最简单的方法是将solidcolorbrush设置为Listbox上的资源:

    <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?

    【讨论】:

      猜你喜欢
      • 2010-09-26
      • 1970-01-01
      • 2012-03-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-07
      • 1970-01-01
      • 1970-01-01
      • 2011-01-16
      相关资源
      最近更新 更多