views:

536

answers:

1

Hi all,
I'm trying to set a global style for all the listboxes in my application. Below is the xaml code that i've used. Here i've tried to trigger out an animation but it doesn't work. I just want an animation on the selected item. Any help?

<Style TargetType="{x:Type ListView}">
    <Style.Setters>
        <Setter Property="BorderThickness" Value="5" />
        <Setter Property="FontSize" Value="16" />
        <Setter Property="FontFamily" Value="Arial" />
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate x:Name="ListViewItemTemplate">
                    <TextBlock Text="{Binding}" Padding="0,0,5,5"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ControlTemplate.Triggers>
                        <EventTrigger RoutedEvent="ListViewItemBase.Selected">
                            <BeginStoryboard>
                                <Storyboard TargetProperty="Color">
                                    <ColorAnimation To="#FFFF0000" Duration="0:0:1" AutoReverse="true" />
                                </Storyboard>
                            </BeginStoryboard>
                        </EventTrigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style.Setters>
</Style>

Working Version:

<Style TargetType="{x:Type ListView}"> 
<Style.Setters> 
    <Setter Property="BorderThickness" Value="5" /> 
    <Setter Property="FontSize" Value="16" /> 
    <Setter Property="FontFamily" Value="Arial" /> 
    <Setter Property="ItemTemplate"> 
        <Setter.Value> 
            <DataTemplate x:Name="ListViewItemTemplate"> 
                <TextBlock Text="{Binding}" Padding="0,0,5,5"/> 
            </DataTemplate> 
        </Setter.Value> 
    </Setter> 
    <Setter Property="ItemContainerStyle"> 
        <Setter.Value> 
            <Style> 
                <Style.Triggers>
                    <Trigger Property="ListViewItem.IsSelected" Value="True"> 
                        <Trigger.EnterActions> 
                            <BeginStoryboard> 
                                <Storyboard Target="ListViewItem" TargetProperty="Background.Color"> 
                                    <ColorAnimation To="Red" Duration="0:0:0.5" AutoReverse="true" /> 
                                </Storyboard> 
                            </BeginStoryboard> 
                        </Trigger.EnterActions> 
                    </Trigger> 
                </Style.Triggers>
            </Style> 
        </Setter.Value> 
    </Setter> 
</Style.Setters> 

+2  A: 

Create ItemContainerStyle for the ListBox and and Add Trigger for ListBoxItem.IsSelected == True

Jobi Joy
Can you explain a little? Any codes?
Veer
Instead of setting the `Template` property, set the `ItemContainerStyle` property. Create a style in there with the trigger Jobi said, and then apply your animation when the trigger is activated.
Benny Jobigan
Thanks i'll try that today and let you know :)
Veer
Hi Jobi and Benny,That really works. But what happens is, the BlueHighlight for the selected item still exists and it hides the background color animation. Any solution for this?
Veer
There will be a Trigger which sets the blue color so you have to remove it. Paste your current working XAML here.
Jobi Joy
Check my edit...
Veer
Hi Benny and jobi,How about this?http://vbcity.com/blogs/xtab/archive/2009/06/28/background-color-for-wpf-listbox-selected-item.aspxHope that'll solve the BlueHighlight issue. Let me check it today
Veer
Yes it is the ControlTemplate showing that blue as default so you need to override the ControlTemplate along with the ItemContainerStyle to avoid that.
Jobi Joy