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:

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

 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}”
    <ContentPresenter HorizontalAlignment=”{TemplateBinding HorizontalContentAlignment}”
                                VerticalAlignment=”{TemplateBinding VerticalContentAlignment}”
                                SnapsToDevicePixels=”{TemplateBinding SnapsToDevicePixels}”/>

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

    <!–Related to Selection –>
        <Condition Property=”IsSelected”
        <Condition Property=”Selector.IsSelectionActive”
        <Setter TargetName=”Bd”
                    Value=”{DynamicResource {x:Static SystemColors.ControlBrushKey}}”/>
        <Setter Property=”Foreground”
                    Value=”{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}”/>
    <Trigger Property=”IsEnabled”
      <Setter Property=”Foreground”
                  Value=”{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}”/>

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>