简体   繁体   English

设置背景色或WPF(4.0)列表框-Windows 8

[英]Setting Background Color or WPF (4.0) ListBox - Windows 8

I am attempting to set the background color of a selected ListBoxItem to be white rather than the system color. 我试图将选定的ListBoxItem的背景色设置为白色,而不是系统颜色。 I have read what I could find here on SO and have followed, or believed to have followed the recommendations there ( Change background color for selected ListBox item , WPF How to change the listbox selected item text color when the list box loses focus , Change selected and unfocused Listbox style to not be grayed out , and others). 我已经阅读了在SO上可以找到的内容,并且已经遵循或相信已经遵循了那里的建议( 更改 列表框选定项目的 背景颜色WPF如何在列表框失去焦点时更改列表框选定项目的文本颜色更改选定内容和未聚焦的列表框样式不会变灰 ,等等)。

All seem to solve the problem by setting the HighlightBrush and ControlBrush to Transparent for the selected item. 所有人似乎都可以通过将所选项目的HighlightBrush和ControlBrush设置为Transparent来解决问题。 I have the following XAML and it sets the font color properly, but the backgroound is the default transparent blue regardless of the brush settings. 我有以下XAML,它可以正确设置字体颜色,但是无论画笔设置如何,backgroound都是默认的透明蓝色。 I am still a bit of a WPF noob, so I must be missing something simple here. 我还是有点WPF菜鸟,所以我在这里肯定缺少一些简单的东西。

<ListBox Width="Auto" Height="Auto" Grid.Column="0" BorderThickness="0" Background="#FFF3F3F3" xmlns:sys="clr-namespace:System;assembly=mscorlib">
    <ListBox.ItemsSource>
       <x:Array Type="{x:Type sys:String}">
          <sys:String>String 1</sys:String>
          <sys:String>String 2</sys:String>
          <sys:String>String 3</sys:String>
          <sys:String>String 4</sys:String>
       </x:Array>
    </ListBox.ItemsSource>
    <ListBox.ItemContainerStyle>
       <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
          <Style.Resources>
             <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
             <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
          </Style.Resources>
          <Setter Property="HorizontalContentAlignment" Value="Stretch" />
          <Setter Property="FontSize" Value="16"/>
          <Setter Property="Foreground" Value="#999999"/>
          <Style.Triggers>
             <Trigger Property="IsSelected" Value="True" >
                <Setter Property="Background" Value="White" />
                <Setter Property="Foreground" Value="Black" />
             </Trigger>
          </Style.Triggers>
       </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
       <DataTemplate>
          <TextBlock Text="{Binding}" HorizontalAlignment="Right" Margin="0,0,8,0" Background="Transparent"/>
       </DataTemplate>
    </ListBox.ItemTemplate>
 </ListBox>

I would appreciate any nudges in the right direction. 我会感激任何朝着正确方向前进的人。

EDIT : 编辑

After reading the first answer that it worked for them with a slight change, I took the application that I have been developing on my Windows 8 machine and executed it in a Windows 7 VM and it worked as expected. 在阅读了对他们有用的第一个答案之后,我做了一点改动,然后将我在Windows 8机器上开发的应用程序拿到Windows 7 VM上执行,并且按预期工作。 Any ideas on what needs to change to get this to work on a Windows 8 machine as well as a Windows 7? 关于需要进行哪些更改才能使其在Windows 8机器和Windows 7上运行的任何想法?

Those posts are getting outdated for Windows-8. 这些帖子在Windows-8中已经过时了。

In Windows-8 for some reason Microsoft don't want people editing their Default Style 's so easily or something with a Brush over-ride. 在Windows-8中,出于某些原因,Microsoft不希望人们如此轻松地编辑其Default Style或带有Brush覆盖的内容。

ListBoxItem default Style from VS has this for selection triggers: VS的ListBoxItem默认Style具有以下选择触发器:

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="False" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="#3DDADADA" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="#FFDADADA" />
</MultiTrigger>
<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="True" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="#3D26A0DA" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="#FF26A0DA" />
</MultiTrigger>

Triggers for Selection state no longer are applying brushes we can over-ride easily but are static colors. 选择状态的触发器不再使用画笔,我们可以轻松改写它们,但是它们是静态颜色。 Hence to modify it you are going to need to derive the template and modify the trigger there. 因此,要修改它,您将需要派生模板并在那里修改触发器。 to White White

This is the full Style given by VS2012 Windows-8 for ListBoxItem 这是VS2012 Windows-8为ListBoxItem给出的完整样式

<Style x:Key="ListBoxItemStyle1"
       TargetType="{x:Type ListBoxItem}">
  <Setter Property="SnapsToDevicePixels"
          Value="True" />
  <Setter Property="Padding"
          Value="4,1" />
  <Setter Property="HorizontalContentAlignment"
          Value="{Binding HorizontalContentAlignment,
                          RelativeSource={RelativeSource FindAncestor,
                                                         AncestorLevel=1,
                                                         AncestorType={x:Type ItemsControl}}}" />
  <Setter Property="VerticalContentAlignment"
          Value="{Binding VerticalContentAlignment,
                          RelativeSource={RelativeSource FindAncestor,
                                                         AncestorLevel=1,
                                                         AncestorType={x:Type ItemsControl}}}" />
  <Setter Property="Background"
          Value="Transparent" />
  <Setter Property="BorderBrush"
          Value="Transparent" />
  <Setter Property="BorderThickness"
          Value="1" />
  <Setter Property="FocusVisualStyle">
    <Setter.Value>
      <Style>
        <Setter Property="Control.Template">
          <Setter.Value>
            <ControlTemplate>
              <Rectangle Margin="2"
                         SnapsToDevicePixels="True"
                         Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"
                         StrokeDashArray="1 2"
                         StrokeThickness="1" />
            </ControlTemplate>
          </Setter.Value>
        </Setter>
      </Style>
    </Setter.Value>
  </Setter>
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ListBoxItem}">
        <Border x:Name="Bd"
                Background="{TemplateBinding Background}"
                BorderBrush="{TemplateBinding BorderBrush}"
                BorderThickness="{TemplateBinding BorderThickness}"
                Padding="{TemplateBinding Padding}"
                SnapsToDevicePixels="True">
          <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            Content="{TemplateBinding Content}"
                            ContentStringFormat="{TemplateBinding ContentStringFormat}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
        </Border>
        <ControlTemplate.Triggers>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="IsMouseOver"
                         Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                    Property="Background"
                    Value="#1F26A0DA" />
            <Setter TargetName="Bd"
                    Property="BorderBrush"
                    Value="#A826A0DA" />
          </MultiTrigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="Selector.IsSelectionActive"
                         Value="False" />
              <Condition Property="IsSelected"
                         Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                    Property="Background"
                    Value="#3DDADADA" />
            <Setter TargetName="Bd"
                    Property="BorderBrush"
                    Value="#FFDADADA" />
          </MultiTrigger>
          <MultiTrigger>
            <MultiTrigger.Conditions>
              <Condition Property="Selector.IsSelectionActive"
                         Value="True" />
              <Condition Property="IsSelected"
                         Value="True" />
            </MultiTrigger.Conditions>
            <Setter TargetName="Bd"
                    Property="Background"
                    Value="#3D26A0DA" />
            <Setter TargetName="Bd"
                    Property="BorderBrush"
                    Value="#FF26A0DA" />
          </MultiTrigger>
          <Trigger Property="IsEnabled"
                   Value="False">
            <Setter TargetName="Bd"
                    Property="TextElement.Foreground"
                    Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" />
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

if you modify those triggers to: 如果您将这些触发器修改为:

<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="False" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="White" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="White" />
</MultiTrigger>
<MultiTrigger>
  <MultiTrigger.Conditions>
    <Condition Property="Selector.IsSelectionActive"
                Value="True" />
    <Condition Property="IsSelected"
                Value="True" />
  </MultiTrigger.Conditions>
  <Setter TargetName="Bd"
          Property="Background"
          Value="White" />
  <Setter TargetName="Bd"
          Property="BorderBrush"
          Value="White" />
</MultiTrigger>

you should have your issue sorted. 您应该对您的问题进行排序。

Adding the following trigger to my Item DataTemplate, worked for Windows 10: 将以下触发器添加到我的Item DataTemplate,适用于Windows 10:

<DataTemplate x:Key="MyItemTemplate">
    <Border Name="Border" Background="Transparent" BorderBrush="LightGray" BorderThickness="0,1,0,0" Padding="0">
        <TextBlock Text="{Binding Text}" HorizontalAlignment="Left" FontWeight="Medium" />
    </Border>
    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
            <Setter TargetName="Border" Property="Background" Value="SkyBlue"/>
        </DataTrigger>
    </DataTemplate.Triggers>
</DataTemplate>

So you simply want to make the background of the selected item white? 因此,您只是想将所选项目的背景设为白色?

Your code minus the ControlBrushKey setting works for me: 您的代码减去ControlBrushKey设置对我有用:

<Style.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="White" />
</Style.Resources>

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

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