WPF – Selected Item Style

January 15, 2009

tags: ,
no comments

I got around building a decent amount of controls that derived from ListBox.


The default template of a ListBoxItem contains default style for the selection triggered by various triggers.


If you wish to control the colors, you can adjust it by the following:


<ListBox.Resources>
  <SolidColorBrush x:Key=”{x:Static SystemColors.HighlightBrushKey}” Color=”Transparent” />
  <SolidColorBrush x:Key=”{x:Static SystemColors.HighlightTextBrushKey}” Color=”Black” />
</ListBox.Resources>


 If you wish to alter the selection/any other actual behavior:


you can replave the default template of the ListBoxItem and edit anything you like, the following example is a duplicate of the default template with comments on what you should remove if you like to disable selection highlighting.


<ControlTemplate TargetType=”{x:Type ListBoxItem}”>
  <Border Name=”Bd”
               Background=”{TemplateBinding Background}”
               BorderBrush=”{TemplateBinding BorderBrush}”
               BorderThickness=”{TemplateBinding BorderThickness}”
               Padding=”{TemplateBinding Padding}”
               SnapsToDevicePixels=”true”>
    <ContentPresenter HorizontalAlignment=”{TemplateBinding HorizontalContentAlignment}”
                                VerticalAlignment=”{TemplateBinding VerticalContentAlignment}”
                                SnapsToDevicePixels=”{TemplateBinding SnapsToDevicePixels}”/>
  </Border>


  <ControlTemplate.Triggers>
    <!–Related to Selection –>
    <Trigger Property=”IsSelected”
                 Value=”true”>
      <Setter TargetName=”Bd”
                  Property=”Background”
                  Value=”{DynamicResource {x:Static SystemColors.HighlightBrushKey}}”/>
      <Setter Property=”Foreground”
                  Value=”{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}”/>
    </Trigger>


    <!–Related to Selection –>
    <MultiTrigger>
      <MultiTrigger.Conditions>
        <Condition Property=”IsSelected”
                         Value=”true”/>
        <Condition Property=”Selector.IsSelectionActive”
                         Value=”false”/>
      </MultiTrigger.Conditions>
        <Setter TargetName=”Bd”
                    Property=”Background”
                    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>

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*