Welcome to WindowsClient.net | Sign in | Join

Zuker On Foundations

The realm of .NET (WPF, WCF and all around)

WPF - Selected Item Style

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>

Posted: Feb 20 2008, 08:06 AM by zuker | with no comments
Filed under:

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required) 

Page view counter