Whirlwind991
Whirlwind991

Reputation: 495

Permenant style change in XAML

I have a simple style set up for a grid:

 <Style TargetType="Grid" x:Key="Block">                  
 <Setter Property="Background" Value="#363636" />      
    <Style.Triggers>

        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="#555555" />
        </Trigger>

    </Style.Triggers>
</Style>

So the grid reverts back to its normal background colour when the mouse leaves. But what I'm aiming for is I would like the grid background colour to permanently stay on #555555 after the mouse leaves the grid. Can I do this via XAML or does it have to be done in C#?

Upvotes: 1

Views: 50

Answers (1)

lokusking
lokusking

Reputation: 7456

You can do this by using an EventTrigger instead of using a normal one.

In the following example, i'll illustrate how this is done:

<Grid Background="#363636">
        <Grid.Resources>
            <Storyboard x:Key="OnMouseEnter">
                <ColorAnimationUsingKeyFrames Storyboard.TargetProperty="Background.Color" >
                    <DiscreteColorKeyFrame KeyTime="0" Value="#555555"></DiscreteColorKeyFrame>
                </ColorAnimationUsingKeyFrames>
            </Storyboard>
        </Grid.Resources>
        <Grid.Triggers>
            <EventTrigger RoutedEvent="UIElement.MouseEnter">
                <BeginStoryboard Storyboard="{StaticResource OnMouseEnter}"/>
            </EventTrigger>
        </Grid.Triggers>
</Grid>

This might give you a clue on how it can be done in pure XAML.

Upvotes: 1

Related Questions