2009-01-29 211 views
5

當用戶點擊一個ListBoxItem,我想它是 一個大膽 較大 字體紅色 背景黃色爲什麼我不能在WPF中設置選定ListBoxItem的背景顏色?

一切正常,除了背景。 似乎有一個標準(藍色)的背景爲選定的項目。 如何覆蓋並將選定的背景變成黃色?

下面是代碼:

<Window x:Class="AlternateListBox2.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300" 
    xmlns:local="clr-namespace:AlternateListBox2"> 
    <Window.Resources> 
     <local:Countries x:Key="countries"/> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Setter Property="Content" Value="{Binding Path=Name}"/> 
      <Setter Property="Margin" Value="2"/> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="FontWeight" Value="Bold"/> 
        <Setter Property="FontSize" Value="18"/> 
        <Setter Property="Background" Value="Yellow"/> 
        <Setter Property="Foreground" Value="Red"/> 
       </Trigger> 
      </Style.Triggers> 

     </Style> 
    </Window.Resources> 
    <StackPanel> 
     <ListBox 
      ItemsSource="{Binding Source={StaticResource countries}}" 
      Width="100" 
      Margin="10" 
      HorizontalAlignment="Left" 
      /> 
    </StackPanel> 
</Window> 

回答

12

它可以做得更簡單。所選ListBox項目的背景顏色來自SystemColors。因此,您需要做的是覆蓋ListBox資源中的SystemColors:

<ListBox.Resources> 
    <!--Selected color when the ListBox is focused--> 
    <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Yellow" /> 
    <!--Selected color when the ListBox is not focused--> 
    <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Yellow" /> 
</ListBox.Resources> 
+3

無法在.net 4.5中使用,請參閱http://stackoverflow.com/questions/12710296/overriding-listboxitem-background-color-when-not-in-focus-net-4-5/12710338 – 2012-10-03 15:23:24

2

此代碼應設置後臺工作。問題是您需要創建一個ControlTemplate並將ContentPresenter的Background屬性賦值爲「Yellow」。

<Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
        <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/> 
          <Setter Property="Background" TargetName="Bd" Value="Yellow"/> 
          <Setter Property="FontWeight" Value="Bold"/> 
          <Setter Property="FontSize" Value="18"/> 
          <Setter Property="Foreground" Value="Red"/> 
         </Trigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsSelected" Value="true"/> 
           <Condition Property="Selector.IsSelectionActive" Value="false"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> 
         </MultiTrigger> 
         <Trigger Property="IsEnabled" Value="false"> 
          <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
0

感謝Frances !!這對我來說確實有點。這裏是我的代碼,它允許模板爲選定和未選擇的列表項目使用「StrColor」屬性。

<Style TargetType="ListBoxItem"> 
     <Setter Property="HorizontalContentAlignment" Value="Center"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <!--Nice Brush--> 
     <Setter Property="Background"> 
      <Setter.Value> 
       <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
        <!-- This is a gradient from white to StrColor and back to white --> 
        <!--<GradientStop Color="White" Offset="0"/> 
        <GradientStop Color="{Binding Path=StrColor}" Offset="0.445"/> 
        <GradientStop Color="White" Offset="1"/> 
        <GradientStop Color="{Binding Path=StrColor}" Offset="0.53"/>--> 
        <!-- This is a gradient from StrColor to white --> 
        <GradientStop Color="{Binding Path=StrColor}" Offset="0"/> 
        <GradientStop Color="White" Offset="1"/> 
       </LinearGradientBrush> 
      </Setter.Value> 
     </Setter> 
     <!--Standard Color--> 
     <!--<Setter Property="Background" Value="{Binding Path=StrColor}"/>--> 
     <Setter Property="Foreground" Value="{Binding Path=TextColor}"/> 
     <Setter Property="Height" Value="{Binding Path=Height}"/> 
     <Setter Property="Margin" Value="0"/> 
     <Setter Property="Focusable" Value="False"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
        <Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="contentPresenter"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsSelected" Value="true"> 
          <Setter Property="OpacityMask" TargetName="contentPresenter" Value="{x:Null}"/> 
          <Setter Property="Background" TargetName="Bd"> 
           <Setter.Value> 
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0"> 
             <GradientStop Color="{Binding Path=StrColor}" Offset="0"/> 
             <GradientStop Color="White" Offset="1"/> 
            </LinearGradientBrush> 
           </Setter.Value> 
          </Setter> 
          <Setter Property="Foreground" Value="{Binding Path=TextColor}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</UserControl.Resources> 
2

「這是可以做到簡單了很多。選定 列表框項目的背景顏色從systemColors中取出。所以,你需要 做的是覆蓋systemColors中的ListBox的資源「

覆蓋SystemColors,ListBoxItem模板將用於背景/前景的概念很糟糕,並且經常會混淆對WPF不熟悉的人。 因此,我的建議是重寫ListBoxItem模板並對其進行自定義。