Click here to Skip to main content
14,767,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all.
First of all I am sorry for my language grammar because my first language is Persian (Iran).
This is all my code:
<Storyboard x:Key="ButtonMouseEnter" Storyboard.TargetName="UploadButton" AutoReverse="False" BeginTime="0:0:0"  SpeedRatio="3">
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
        <DiscreteObjectKeyFrame KeyTime="0:0:0.0">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#e9f1cc" Offset="0"/>
                    <GradientStop Color="#cbd3ae" Offset="0.5"/>
                    <GradientStop Color="#858d68" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.1">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#dfe7c2" Offset="0"/>
                    <GradientStop Color="#c8d0ab" Offset="0.5"/>
                    <GradientStop Color="#8f9772" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.2">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#d5ddb8" Offset="0"/>
                    <GradientStop Color="#c5cda8" Offset="0.5"/>
                    <GradientStop Color="#99a17c" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#cbd3ae" Offset="0"/>
                    <GradientStop Color="#c2caa5" Offset="0.5"/>
                    <GradientStop Color="#a3ab86" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.4">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#c1c9a4" Offset="0"/>
                    <GradientStop Color="#bfc7a2" Offset="0.5"/>
                    <GradientStop Color="#adb590" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#b7bf9a" Offset="0"/>
                    <GradientStop Color="#bcc49f" Offset="0.5"/>
                    <GradientStop Color="#b7bf9a" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#adb590" Offset="0"/>
                    <GradientStop Color="#b9c19c" Offset="0.5"/>
                    <GradientStop Color="#c1c9a4" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.7">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#a3ab86" Offset="0"/>
                    <GradientStop Color="#b6be99" Offset="0.5"/>
                    <GradientStop Color="#cbd3ae" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.8">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#99a17c" Offset="0"/>
                    <GradientStop Color="#b3bb96" Offset="0.5"/>
                    <GradientStop Color="#d5ddb8" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.9">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#8f9772" Offset="0"/>
                    <GradientStop Color="#b0b893" Offset="0.5"/>
                    <GradientStop Color="#dfe7c2" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:1.0">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#858d68" Offset="0"/>
                    <GradientStop Color="#adb590" Offset="0.5"/>
                    <GradientStop Color="#e9f1cc" Offset="1"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>
<Storyboard x:Key="ButtonMouseLeave" Storyboard.TargetName="UploadButton" AutoReverse="False" BeginTime="0:0:0"  SpeedRatio="3">
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background">
        <DiscreteObjectKeyFrame KeyTime="0:0:0.0">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#e9f1cc" Offset="1"/>
                    <GradientStop Color="#adb590" Offset="0.5"/>
                    <GradientStop Color="#858d68" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.1">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#dfe7c2" Offset="1"/>
                    <GradientStop Color="#b0b893" Offset="0.5"/>
                    <GradientStop Color="#8f9772" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.2">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#d5ddb8" Offset="1"/>
                    <GradientStop Color="#b3bb96" Offset="0.5"/>
                    <GradientStop Color="#99a17c" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.3">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#cbd3ae" Offset="1"/>
                    <GradientStop Color="#b6be99" Offset="0.5"/>
                    <GradientStop Color="#a3ab86" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.4">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#c1c9a4" Offset="1"/>
                    <GradientStop Color="#b9c19c" Offset="0.5"/>
                    <GradientStop Color="#adb590" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#b7bf9a" Offset="1"/>
                    <GradientStop Color="#bcc49f" Offset="0.5"/>
                    <GradientStop Color="#b7bf9a" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.6">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#adb590" Offset="1"/>
                    <GradientStop Color="#bfc7a2" Offset="0.5"/>
                    <GradientStop Color="#c1c9a4" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.7">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#a3ab86" Offset="1"/>
                    <GradientStop Color="#c2caa5" Offset="0.5"/>
                    <GradientStop Color="#cbd3ae" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.8">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#99a17c" Offset="1"/>
                    <GradientStop Color="#c5cda8" Offset="0.5"/>
                    <GradientStop Color="#d5ddb8" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:0.9">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#8f9772" Offset="1"/>
                    <GradientStop Color="#c8d0ab" Offset="0.5"/>
                    <GradientStop Color="#dfe7c2" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
        <DiscreteObjectKeyFrame KeyTime="0:0:1.0">
            <DiscreteObjectKeyFrame.Value>
                <LinearGradientBrush  EndPoint="0,1" StartPoint="0,0">
                    <GradientStop Color="#858d68" Offset="1"/>
                    <GradientStop Color="#cbd3ae" Offset="0.5"/>
                    <GradientStop Color="#e9f1cc" Offset="0"/>
                </LinearGradientBrush>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>
</Storyboard>
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="BorderBrush" Value="#FF707070" />
    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="Padding" Value="1"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border Width="69" Background="{StaticResource NormalBackground}"
        BorderBrush="#d2d8ca" BorderThickness="0.6" CornerRadius="15" x:Name="Button">
                    <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" RecognizesAccessKey="True"/>
                </Border>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>


How can i convert the following XAML code into C # code

<ControlTemplate.Triggers>
    <Trigger Property="IsMouseOver" Value="true">
        <Trigger.EnterActions>
            <BeginStoryboard Storyboard="{StaticResource ButtonMouseEnter}"/>
        </Trigger.EnterActions>
        <Trigger.ExitActions>
            <BeginStoryboard Storyboard="{StaticResource ButtonMouseLeave}"/>
        </Trigger.ExitActions>
    </Trigger>
    <Trigger Property="IsFocused" Value="true">
        <Trigger.EnterActions>
            <BeginStoryboard Storyboard="{StaticResource ButtonMouseEnter}"/>
        </Trigger.EnterActions>
        <Trigger.ExitActions>
            <BeginStoryboard Storyboard="{StaticResource ButtonMouseLeave}"/>
        </Trigger.ExitActions>
    </Trigger>
</ControlTemplate.Triggers>


like this:

private void UploadButton_MouseEnter(object sender, MouseEventArgs e)
{
(Resources["UploadButtonMouseEnter"] as Storyboard).Begin();
}


Thanks.

What I have tried:

private void UploadButton_MouseEnter(object sender, MouseEventArgs e)
{
Trigger trigger = new Trigger();            
}
Posted
Updated 4 days ago
v7

1 solution

In this case, the "event" (Mouse Enter; whatever) IS the "trigger".

There is nothing else to do except start the storyboard (SB).

Your "new Trigger()" code has no place here.

Based on your XAML, you need event handlers for MouseOver and GotFocus; which run the (SB) resources ButtonMouseEnter and ButtonMouseLeave.
   
Comments
Reza jafery 4 days ago
   
My XAML code work correctly , but in C# event doesn't work and It does not show any errors.
I tested this (Resources["UploadButtonMouseEnter"] as Storyboard).Begin();
But it did not work
Gerry Schmitz 4 days ago
   
Where's your "button" in the XAML? You've shown a StoryBoard and a template, but no buttons or controls.

Maybe you should just make it all code or XAML; you're confusing yourself.

https://docs.microsoft.com/en-us/dotnet/desktop/wpf/graphics-multimedia/how-to-animate-a-property-by-using-a-storyboard?view=netframeworkdesktop-4.8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900