Changing a ListBox's Selection Style

When you want to change the default selection style on a ListBox it's fairly straight forward. All you need to do is create a control template and use triggers to set values when IsSelected is true.

Custom ListBox Selection Style

<Style TargetType="ListBoxItem">

  <Setter Property="Template">

    <Setter.Value>

      <ControlTemplate TargetType="ListBoxItem">

        <Border x:Name="ItemBorder"

                BorderBrush="Black"

                Background="LightGray"

                BorderThickness="2"

                CornerRadius="4"

                Margin="3">

          <ContentPresenter Margin="2" />

        </Border>

        <ControlTemplate.Triggers>

          <Trigger Property="IsSelected" Value="True">

            <Setter TargetName="ItemBorder"

                    Property="BorderBrush"

                    Value="Red"/>

          </Trigger>

          <Trigger Property="IsMouseOver" Value="True">

            <Setter TargetName="ItemBorder"

                    Property="BorderBrush"

                    Value="Blue" />

          </Trigger>

          <MultiTrigger>

            <MultiTrigger.Conditions>

              <Condition Property="IsMouseOver" Value="False" />

              <Condition Property="IsSelected" Value="False" />

            </MultiTrigger.Conditions>

            <Setter TargetName="ItemBorder"

                    Property="Opacity"

                    Value="0.50" />

          </MultiTrigger>

        </ControlTemplate.Triggers>

      </ControlTemplate>

    </Setter.Value>

  </Setter>

</Style>

 

<ListBox>

  <ListBoxItem>One</ListBoxItem>

  <ListBoxItem>Two</ListBoxItem>

  <ListBoxItem>Three</ListBoxItem>

  <ListBoxItem>Four</ListBoxItem>

  <ListBoxItem>Five</ListBoxItem>

  <ListBoxItem>Six</ListBoxItem>

  <ListBoxItem>Seven</ListBoxItem>

  <ListBoxItem>Eight</ListBoxItem>

</ListBox>

Published Monday, June 11, 2007 10:30 AM by Joe
Filed under:

Comments

# Using WPF With VSTO & Office 2007

Tuesday, July 17, 2007 9:00 PM by Joe's Blog

Hosting Windows Forms controls in Office 2007 Custom Task Pane's is pretty simple, as shown in this MSDN

# Using WPF With VSTO & Office 2007

Friday, September 14, 2007 12:16 AM by Joe's Blog

Hosting Windows Forms controls in Office 2007 Custom Task Pane&#39;s is pretty simple, as shown in this

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Powered by Community Server (Non-Commercial Edition), by Telligent Systems