【问题标题】:Listbox WPF item background color列表框 WPF 项目背景颜色
【发布时间】:2013-03-15 11:21:39
【问题描述】:

我想改变 ListBoxItem 的背景颜色

搜索后我决定使用这个

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

        </ListBox.Resources>
        <TextBlock>fdfsdf1</TextBlock>
        <TextBlock>fdfsdf3</TextBlock>
        <TextBlock>fdfsdf5</TextBlock>
        <TextBlock>fdfsdf3</TextBlock>
        <TextBlock>fdfsdf4</TextBlock>
    </ListBox>

当一个ListBoxItem专注时,背景是蓝色的预期,但是当所选的ListBoxItem丢失焦点时,背景会变为灰色。如何让背景失去焦点时保持蓝色?

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    如果你的意思是当它被选中但不活动时尝试InactiveSelectionHighlightBrushKey

        <ListBox.Resources>
            <!-- Background of selected item when focussed -->
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                             Color="Blue" />
            <!-- Background of selected item when not focussed -->
            <SolidColorBrush x:Key="{x:Static SystemColors.InactiveSelectionHighlightBrushKey}"
                             Color="Blue" />
    
        </ListBox.Resources>
    

    【讨论】:

    • 是的,但我认为我们都以相同的方式阅读了这个问题,哈哈,我认为 OP 只想要InactiveSelectionHighlightBrushKey
    【解决方案2】:

    试试这个

    <ListBox>
            <ListBox.Resources>
                <Style TargetType="ListBoxItem">
                    <Setter Property="Background" Value="Blue" />
                </Style>
            </ListBox.Resources>
            <TextBlock>fdfsdf1</TextBlock>
            <TextBlock>fdfsdf3</TextBlock>
            <TextBlock>fdfsdf5</TextBlock>
            <TextBlock>fdfsdf3</TextBlock>
            <TextBlock>fdfsdf4</TextBlock>
        </ListBox>
    

    【讨论】:

    • 如果我这样做,我会将所有列表框项目变为蓝色。我的任务被选中 listboxitem 在焦点和散焦状态必须是蓝色的。但是我在聚焦时会变成蓝色,而在散焦时会变成灰色
    【解决方案3】:

    如果您认为系统颜色键不适合您,那么您可以通过为 ListboxItems 创建新样式来强制它,如下所示。

            <Style TargetType="{x:Type ListBoxItem}">
                <Setter Property="Background" Value="Silver"/>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Red"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
    

    【讨论】:

      【解决方案4】:

      这是我用来为 ListBox 的活动/非活动项选择颜色的方法:

      <ListBox Name="lbExample" SelectionMode="Multiple">
          <ListBox.ItemTemplate>
              <DataTemplate>
                  <...>
              </DataTemplate>
          </ListBox.ItemTemplate>
          <ListBox.Resources>
                  <!-- Background of selected item when not focussed -->
                  <Style TargetType="{x:Type ListBoxItem}">
                      <Setter Property="Background" Value="AntiqueWhite" />
                  </Style>
      
                  <!-- Background of selected item when focussed -->
                  <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightGreen" />
          </ListBox.Resources>
      </ListBox>
      

      【讨论】:

        猜你喜欢
        • 2014-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多