2010-01-26 233 views
55

这是我的XAML到目前为止。更改选定列表框项目的背景颜色

<ScrollViewer Grid.Column="1" Grid.RowSpan="2"> 

     <ListBox Background="Black" ItemsSource="{Binding Path=ActiveLog}" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <Grid Background="Black"> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="200"></ColumnDefinition> 
          <ColumnDefinition Width="*"></ColumnDefinition> 
         </Grid.ColumnDefinitions> 
         <Grid.RowDefinitions> 
          <RowDefinition></RowDefinition> 
          <RowDefinition></RowDefinition> 
         </Grid.RowDefinitions> 
         <TextBlock Grid.Column="0" Grid.Row="0" Foreground="White"> 
          <TextBlock >Date:</TextBlock> 
          <TextBlock Text="{Binding Path=LogDate}"/> 
         </TextBlock> 
         <TextBlock Grid.Column="1" Grid.Row="0" Foreground="White"> 
          <TextBlock >Severity:</TextBlock> 
          <TextBlock Text="{Binding Path=Severity}"/> 
         </TextBlock> 
         <TextBlock Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Foreground="LightGray" Text="{Binding Path=Message}"></TextBlock> 
        </Grid> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
      <ListBox.Template> 
       <ControlTemplate> 
        <StackPanel Background="Black" IsItemsHost="True" > 
        </StackPanel> 
       </ControlTemplate> 
      </ListBox.Template> 

     </ListBox> 
    </ScrollViewer> 

唯一的问题是所选项目右边有一个蓝色框。我认为有一种方法可以改变选择颜色,但我找不到它。

回答

38

您需要使用ListBox.ItemContainerStyle

ListBox.ItemTemplate指定应如何显示项目的内容。但是WPF仍然将每个项目包装在一个ListBoxItem控件中,默认情况下,它的背景设置为系统高亮颜色(如果选中)。你不能停止WPF创建ListBoxItem控件,但你可以设置它们的样式 - 在你的情况下,将Background设置为始终为透明或黑色或其他 - 然后使用ItemContainerStyle。

juFo's answer显示了一种可能的实现,通过“劫持”的项目风格的范围内的制度背景画笔资源;另一种可能更习惯的技术是使用Setter作为Background属性。

+0

好吧,现在它变得更有意义。谢谢。 – 2010-01-26 08:56:38

+0

如果您在ItemContainerStyle中为'IsSelected'中的'Background'属性使用'Setter'属性为true,那么它不起作用。它仍然使用系统高亮颜色。 :( – user1108069 2015-11-26 08:14:57

+0

要更改选定的ListBoxItem的背景颜色,您需要重新模板ListBoxItem。ref:在[这里]接受的答案中的评论(http://stackoverflow.com/questions/7059526/set-background-color- VS 2012,.Net Framework 4.5。 – user1108069 2015-11-26 09:05:09

66
<UserControl.Resources> 
    <Style x:Key="myLBStyle" TargetType="{x:Type ListBoxItem}"> 
     <Style.Resources> 
      <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" 
          Color="Transparent"/> 
     </Style.Resources> 
    </Style> 
</UserControl.Resources> 

<ListBox ItemsSource="{Binding Path=FirstNames}" 
     ItemContainerStyle="{StaticResource myLBStyle}"> 

你只覆盖一个ListBoxItem的样式(见:TargetType的是一个ListBoxItem)

+15

这不再适用于在ControlTemplate触发器中使用静态颜色的Windows-8。你必须派生基础'样式'并指定那些触发器中的过度使用刷子或直接给出颜色。 http://stackoverflow.com/a/16820062/1834662 – Viv 2013-05-29 17:17:59

+1

感谢您分享此@Viv – juFo 2013-08-28 08:55:50

+0

@Viv这是否也适用于.net 4.5中的wpf? – Gusdor 2013-10-03 15:34:40

48

或者您可以将HighlightBrushKey直接应用于ListBox。 Setter Property =“Background”Value =“透明”不起作用。但我必须将前景设置为黑色。

<ListBox ... > 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="ListBoxItem"> 
       <Style.Triggers> 
        <Trigger Property="IsSelected" Value="True" > 
         <Setter Property="FontWeight" Value="Bold" /> 
         <Setter Property="Background" Value="Transparent" /> 
         <Setter Property="Foreground" Value="Black" /> 
        </Trigger> 
       </Style.Triggers> 
       <Style.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
       </Style.Resources> 
      </Style>     
     </ListBox.ItemContainerStyle> 
+1

Down vote care to explain。 – Paparazzi 2015-05-04 07:47:59

+0

这个解决方案适用于我,但@ juFo的答案不是,因为所有的控件都给出了transparnt,没有看到任何文字 – IFink 2015-12-15 16:57:34

29

我必须设置HighlightBrushKey和ControlBrushKey才能正确设置样式。否则,虽然它有焦点,但会正确使用透明的HighlightBrusKey。 Bt,如果控件失去焦点(虽然仍然突出显示),那么它使用ControlBrushKey。

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

希望这可以帮助别人。

+0

这对我有很大的帮助,我不知道当ListBox没有被聚焦时SystemColors刷子使用了什么:) +1 – Kyopaxa 2013-03-11 18:01:57

+0

这对于我在选择on时使背景透明至关重要右键点击。谢谢! – 2013-04-30 19:32:15

+15

使用InactiveSelectionHighlightBrushKey代替.NET 4.5中的ControlBrushKey。 – 2013-05-21 15:42:09

7

如果选择不是很重要,最好是用包裹在ScrollViewer中一个ItemsControl。这个组合比Listbox(实际上已经从ItemsControl派生)更加轻量级,并且使用它将消除使用便宜的hack来覆盖已经从ItemsControl中缺少的行为的需要。

如果选择行为非常重要,那么这显然不起作用。但是,如果您想要改变选定项目背景的颜色,使其对用户不可见,那么这只会使其混淆。如果你的意图是改变某些其他特征来表明该项目被选中,那么这个问题的其他一些答案可能仍然更相关。

这里的标记应该是什么样子的骨架:

<ScrollViewer> 
     <ItemsControl> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        ... 
       </DataTemplate> 
      </ItemsControl.ItemTemplate> 
     </ItemsControl> 
    </ScrollViewer> 
5

你必须创建项目选择这样一个新的模板。

<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="ListBoxItem"> 
      <Border 
       BorderThickness="{TemplateBinding Border.BorderThickness}" 
       Padding="{TemplateBinding Control.Padding}" 
       BorderBrush="{TemplateBinding Border.BorderBrush}" 
       Background="{TemplateBinding Panel.Background}" 
       SnapsToDevicePixels="True"> 
       <ContentPresenter 
        Content="{TemplateBinding ContentControl.Content}" 
        ContentTemplate="{TemplateBinding ContentControl.ContentTemplate}" 
        HorizontalAlignment="{TemplateBinding Control.HorizontalContentAlignment}" 
        VerticalAlignment="{TemplateBinding Control.VerticalContentAlignment}" 
        SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> 
      </Border> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 
+6

一个好的答案不仅仅是一个可行的答案,OP应该理解他的错误,而不是仅仅复制粘贴它。否则他/她会在一个星期后回来,在不同的背景下提出同样的问题。 – ShellFish 2015-05-30 00:14:50

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

此代码工作对我来说就像魅力。感谢原始海报。

相关问题