Jan3Sobieski
Jan3Sobieski

Reputation: 275

change selection animation in wpf listbox

I have to change animation in WPF listbox, because I use my own.

enter image description here

The light blue is my selection (I made it with event select_changed and then I changed TextBlock.Background into lightblue)

The blue selection is the default, which I want to be transparent so that the selection will be more elegant.

Does anyone know how to do that?

Here is what I am currently doing:

        foreach (var item in myListBox.Items)
            (item as TextBlock).Background = Brushes.White;
        if (myListBox.SelectedItem != null)
            (myListBox.SelectedItem as TextBlock).Background= Brushes.LightBlue;
        myListBox.ScrollIntoView(myListBox.SelectedItem);

Upvotes: 0

Views: 460

Answers (1)

Sheridan
Sheridan

Reputation: 69979

Just add this Style to your ListBox:

<Style x:Key="HiddenDefaultSelectionStyle" TargetType="{x:Type ListBoxItem}">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent" />
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightTextBrushKey}" Color="Black" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlTextBrushKey}" Color="Black" />
    </Style.Resources>
</Style>

You can replace the colour Transparent with whichever colour you prefer. I believe that the names of these properties are self explanatory.

Upvotes: 1

Related Questions