How to fade in and out buttons except for the text in c# WPF

I have made a button that fades in and out when mouseEnter and mouseLeave.. But my problem is that, the text or content fades in and out also.. I want it to stay.. or is it possible to make the border fade in and out only? not the whole button?

I have this code :

    private void Button_MouseLeave_1(object sender, MouseEventArgs e)
    {
        Button c = (Button)sender;
        DoubleAnimation animation = new DoubleAnimation(0, TimeSpan.FromSeconds(1));
        c.BeginAnimation(Button.OpacityProperty, animation);
    }

    private void Button_MouseEnter_1(object sender, MouseEventArgs e)
    {
        Button c = (Button)sender;
        DoubleAnimation animation = new DoubleAnimation(1, TimeSpan.FromSeconds(1));
        c.BeginAnimation(Button.OpacityProperty, animation);
    }

Upvotes: 1

Views: 592

Answers (2)

Sankarann
Sankarann

Reputation: 2665

Make use of the below Style to get your requirement.

<Window.Resources>

    <Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <LinearGradientBrush x:Key="ButtonNormalBackground" EndPoint="0,1" StartPoint="0,0">
        <GradientStop Color="#F3F3F3" Offset="0"/>
        <GradientStop Color="#EBEBEB" Offset="0.5"/>
        <GradientStop Color="#DDDDDD" Offset="0.5"/>
        <GradientStop Color="#CDCDCD" Offset="1"/>
    </LinearGradientBrush>
    <SolidColorBrush x:Key="ButtonNormalBorder" Color="#FF707070"/>

</Window.Resources>

Above things are going to be resources which was referenced with its Key. It is not mandatory to be in Window's Resources. It should in the Resource of its peer control, so can place it in Grid's Resources also, No problem.

Below Style can also be in the above resources since it was targeted to All the Buttons(If it was targeted directly, this style will apply for all the buttons in that particular window). If this style need to apply only for a particular Button means, you can directly post it inside the <Button.Style> below style </Button.Style>.

<Style TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="{StaticResource ButtonNormalBackground}"/>
        <Setter Property="BorderBrush" Value="{StaticResource ButtonNormalBorder}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <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="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver">
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="Chrome">
                                            <EasingDoubleKeyFrame KeyTime="0:0:0:1" Value="0">
                                                <EasingDoubleKeyFrame.EasingFunction>
                                                    <BackEase EasingMode="EaseOut"/>
                                                </EasingDoubleKeyFrame.EasingFunction>
                                            </EasingDoubleKeyFrame>
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed"/>
                                <VisualState x:Name="Disabled"/>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" BorderBrush="{TemplateBinding BorderBrush}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="{TemplateBinding IsDefaulted}" SnapsToDevicePixels="true"/>
                        <ContentPresenter x:Name="content" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Upvotes: 0

Savaratkar
Savaratkar

Reputation: 2084

I will suggest to put one more label over the button instead of inside the button. Put your content in that label with label background-color as transparent and apply fade-in/out logic on your button.

Upvotes: 2

Related Questions