WPF ListBox Selection Color

user64718 picture user64718 · Apr 27, 2009 · Viewed 29.1k times · Source

Sorry if this has been asked before, but I couldn't find a solution to what I'm looking for in the related questions that popped up, or on Google.

In my application I'm trying to recreate Words New Document dialog, list on the left of items and on the right an icon with text underneath. In Word it has the orange gradient when you mouse over, and a darker gradient when you select an item. I've got most of this recreated, except for changing the background color once you select an item. Here's the code I'm using to create this:

    <ListView Margin="236,34,17,144" Name="listView1" HorizontalContentAlignment="Stretch">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <UniformGrid  Columns="5" IsItemsHost="True" VerticalAlignment="Top" >
                </UniformGrid>
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>
        <ListView.ItemTemplate>
            <DataTemplate >
                <StackPanel HorizontalAlignment="Center" Width="auto">
                    <Image Source="images/document32.png" HorizontalAlignment="Center"/>
                    <TextBlock Text="{Binding}" HorizontalAlignment="Center" />
                </StackPanel>
            </DataTemplate>
        </ListView.ItemTemplate>
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}"  >                 
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="true">
                        <Setter Property="Foreground" Value="Yellow" />
                        <Setter Property="Background" Value="Orange" />
                    </Trigger>
                    <Trigger Property="IsMouseOver" Value="true">
                        <Setter Property="Foreground" Value="Black" />
                        <Setter Property="Background">
                            <Setter.Value>
                                <LinearGradientBrush EndPoint="0.5,1" StartPoint="1,0">
                                    <GradientStop Color="#d3e7ff" Offset="0.986"/>
                                    <GradientStop Color="#b0d2fc" Offset="0.5"/>
                                    <GradientStop Color="#8ec1ff" Offset="0.51"/>
                                </LinearGradientBrush>
                            </Setter.Value>
                        </Setter>
                    </Trigger>

                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView>

So this creates the look I'm going for, does the mouse over, and when I select an item in the listview it will change the fonts text to Yellow, but it refuses to change the background from the default blue to orange, and ideally it would be another gradient anyways and not a floodfilled color. Thanks for any help.

Answer

Micah picture Micah · Apr 27, 2009

There are a few hacks you can do like overriding the system color key, but most likely you will want to provide a new template to achieve this. Here's a fairly nice looking one I put together:

<Style x:Key="ListboxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Margin" Value="1,2,1,1"/>
    <Setter Property="HorizontalAlignment" Value="Stretch" />
    <Setter Property="Background" Value="{StaticResource NormalItemBackground}" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Grid>
                    <Border Background="{TemplateBinding Background}" />
                    <Border Background="#BEFFFFFF" Margin="3,1">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            <Border Margin="2,1,2,0" Grid.Row="0" Background="#57FFFFFF" />
                        </Grid>
                    </Border>
                    <ContentPresenter Margin="8,5" />
                </Grid>
                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsMouseOver" Value="True" />
                            <Condition Property="IsSelected" Value="False"/> 
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="{StaticResource HotItemBackground}" />
                    </MultiTrigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{StaticResource SelectedItemBackground}" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<Style x:Key="ListBoxStyle" TargetType="{x:Type ListBox}">
    <Setter Property="ItemContainerStyle" Value="{DynamicResource ListboxItemStyle}" />
    <Setter Property="Margin" Value="3,3,2,1" />
</Style>