2009-01-29 15 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.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 देखें –

2

इस कोड को पृष्ठभूमि की स्थापना के लिए काम करना चाहिए:

यहाँ कोड है। समस्या यह है कि आपको ControlTemplate बनाने और सामग्री प्रदाता की पृष्ठभूमि संपत्ति को "पीला" मान असाइन करने की आवश्यकता है।

<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

धन्यवाद फ़्रांस !! यह मेरे लिए कुछ हद तक अच्छा था। यहां मेरा कोड है जो टेम्पलेट को चयनित और गैर-चयनित सूची आइटमों के लिए "स्ट्रोकॉलर" प्रॉपर्टी का उपयोग करने की अनुमति देता है।

<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

"यह एक बहुत आसान किया जा सकता है। चयनित ListBox मदों के लिए पृष्ठभूमि रंग SystemColors से लिया जाता है। तो, क्या आप की जरूरत में SystemColors ओवरराइड है अपने ListBox "

अधिभावी SystemColors की अवधारणा के संसाधन, जो ListBoxItem टेम्पलेट पृष्ठभूमि के लिए उपयोग करेगा/अग्रभूमि भयानक है और अक्सर लोग WPF के लिए नए हैं confuses। इस प्रकार, मेरी सिफारिश ListBoxItem टेम्पलेट को ओवरराइड करना और इसे अनुकूलित करना है।