Example Button Link
Can WPF do shape animation likes this button click's effect? I try to use double animation the width and cornerRadius but the effects is not quite good. I using the path animation to animate the progress circle but only the shape animation I really don know how to animate it. Anyone can help me or give some ideas?
This is my Code
<Path x:Name="path" Height="20" Width="20" Stroke="Gray" Stretch="Fill" Opacity="0.3"
StrokeThickness="2" Data="m35,2.5c17.955803,0 32.5,14.544199 32.5,32.5c0,17.955803 -14.544197,32.5 -32.5,32.5c-17.955803,0 -32.5,-14.544197 -32.5,-32.5c0,-17.955801 14.544197,-32.5 32.5,-32.5z"
StrokeDashOffset="204.243713378906">
<Path.StrokeDashArray>
<System:Double>204.243713378906</System:Double>
</Path.StrokeDashArray>
</Path>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard Storyboard.TargetName="bdrTest">
<DoubleAnimationUsingKeyFrames BeginTime="00:00:00"
Storyboard.TargetName="path"
Storyboard.TargetProperty="(Shape.StrokeDashOffset)">
<SplineDoubleKeyFrame KeyTime="00:00:00.1000000" Value="204.243713378906"/>
<SplineDoubleKeyFrame KeyTime="00:00:5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
What you are calling Animations are not Animations. It is simply the Button.Background and Button.Foreground changing colour in response to mouse over events. To give a Button rounded corners, you just need to supply your own ControlTemplate for the Button:
<Button Content="Submit" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border CornerRadius="30" BorderThickness="2" BorderBrush="#FF1ECD97"
TextElement.Foreground="#FF1ECD97" TextElement.FontSize="20"
TextElement.FontWeight="SemiBold" Padding="100,20">
<ContentPresenter />
</Border>
</ControlTemplate>
</Button.Template>
</Button>
Now to add the mouse over effects, we just need to add a Trigger to the ControlTemplate to change the colour of the Button.Background and Button.Foreground when the UIElement.IsMouseOver property is True:
<Button Content="Submit" HorizontalAlignment="Center" VerticalAlignment="Center">
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border Name="Border" CornerRadius="30" Background="Transparent"
BorderThickness="2" BorderBrush="#FF1ECD97" TextElement.Foreground=
"#FF1ECD97" TextElement.FontSize="20" TextElement.FontWeight="SemiBold"
Padding="100,20">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Border" Property="Background"
Value="#FF1ECD97" />
<Setter TargetName="Border" Property="TextElement.Foreground"
Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
The most difficult thing is animating the circular progress. If using codebehind, it's not such problematic. But I tried to use pure XAML. In codebehind we can calculate the Point of some ArcSegment from the corresponding angle and update the ArcSegment accordingly. But in XAML, we have to use some hacky work-around. I had to use 4 layers of borders to create this effect. Firstly I tried with just 2 layers but because changing the Background while animating it simply makes the animation not work expectedly.
I will concentrate mainly on the circular progress effect because this is the most challenging problem when solving with pure XAML. Basically you need some background Border having 2 halves with different colors making a full circle. The left half is grayed, the right half has active (progressed) color. This border is fixed:
You need another Border also with 2 different halves. Its left half is transparent and its right half has active/progressed color. This Border will be rotated (in fact its Background is rotated) in the second phase after the first phase is 180 degree rotated:
The next Border will be used in the first phase. This border also has 2 different halves. The left half is transparent and the right half is grayed:
So initially, these 3 Borders will make a full grayed circle (the left grayed half is of the first Border, the right grayed half is of the third):
In the first phase, the third border is 180 degree rotated. Its grayed half will gradually open the underneath active-color half of the second Border making the effect of progressing from 0 to 180 degree. After that the first phase has been finished. The third Border will be hidden, the second Border will be rotated. Now its active-color half (the right half) will gradually cover the grayed half of the first Border (on the left) and make the effect progressing from 180 to 360 (full of active color).
There are still other Border layers for the last effect of showing tickmark,... Here is the full code. It's fairly long and it's mainly used for reference, you may have to improve it much so that it can be used in your code. I just used simple animations (without easing):
<Button Background="Transparent" Content="Submit" FontSize="30" Foreground="#FF1ECD97" BorderBrush="#FF1ECD97">
<Button.Style>
<Style TargetType="Button">
<Setter Property="Height" Value="80"/>
<Setter Property="Width" Value="280"/>
<Setter Property="BorderThickness" Value="2"/>
</Style>
</Button.Style>
<Button.Template>
<ControlTemplate TargetType="Button">
<ControlTemplate.Resources>
<DrawingBrush Stretch="Fill" x:Key="circle">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Geometry="M-25,-25 h50v50h-50v-50" Brush="Transparent">
</GeometryDrawing>
<GeometryDrawing Geometry="M0,-25 A25,25 0 0 1 0,25">
<GeometryDrawing.Pen>
<Pen Brush="{DynamicResource right}" Thickness="4"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
<GeometryDrawing Geometry="M0,25 A25,25 0 0 1 0,-25">
<GeometryDrawing.Pen>
<Pen Brush="{DynamicResource left}" Thickness="4"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
<DrawingBrush.RelativeTransform>
<RotateTransform Angle="0" CenterX="0.5" CenterY="0.5"/>
</DrawingBrush.RelativeTransform>
</DrawingBrush>
<DrawingBrush x:Key="ok" Stretch="None">
<DrawingBrush.Drawing>
<GeometryDrawing Geometry="M0,20 L10,30 30,5">
<GeometryDrawing.Pen>
<Pen Brush="White" Thickness="5" EndLineCap="Round" StartLineCap="Round"/>
</GeometryDrawing.Pen>
</GeometryDrawing>
</DrawingBrush.Drawing>
<DrawingBrush.RelativeTransform>
<ScaleTransform CenterX="0.5" CenterY=".5" ScaleX="0" ScaleY="0"/>
</DrawingBrush.RelativeTransform>
</DrawingBrush>
</ControlTemplate.Resources>
<Grid>
<Grid.Effect>
<DropShadowEffect ShadowDepth="1"/>
</Grid.Effect>
<Border BorderBrush="{Binding BorderBrush, RelativeSource={RelativeSource TemplatedParent}}"
BorderThickness="{Binding BorderThickness, RelativeSource={RelativeSource TemplatedParent}}" CornerRadius="40"
Background="{Binding Background, RelativeSource={RelativeSource TemplatedParent}}">
</Border>
<Border Opacity="0" Name="b1">
<Border.Resources>
<SolidColorBrush Color="#FF1ECD97" x:Key="right"/>
<SolidColorBrush Color="#ffdddddd" x:Key="left"/>
</Border.Resources>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background" Value="{StaticResource circle}"/>
</Style>
</Border.Style>
</Border>
<Border Opacity="0" Name="b12">
<Border.Resources>
<SolidColorBrush Color="#FF1ECD97" x:Key="right"/>
<SolidColorBrush Color="Transparent" x:Key="left"/>
</Border.Resources>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background" Value="{StaticResource circle}"/>
</Style>
</Border.Style>
</Border>
<Border Opacity="0" Name="b2">
<Border.Resources>
<SolidColorBrush Color="#ffdddddd" x:Key="right"/>
<SolidColorBrush Color="Transparent" x:Key="left"/>
</Border.Resources>
<Border.Style>
<Style TargetType="Border">
<Setter Property="Background" Value="{StaticResource circle}"/>
</Style>
</Border.Style>
</Border>
<ContentPresenter VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Name="cp"/>
<Border Opacity="0" Name="b3_" Background="#FF1ECD97" CornerRadius="40"/>
<Border Opacity="0" Name="b3" Background="{StaticResource ok}"/>
</Grid>
<ControlTemplate.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard Name="bg">
<Storyboard Duration="0:0:6" FillBehavior="Stop">
<DoubleAnimation Storyboard.TargetProperty="Width" To="80" Duration="0:0:.2">
</DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="b1" Storyboard.TargetProperty="Opacity" BeginTime="0:0:.2" To="1" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="b2" Storyboard.TargetProperty="Opacity" BeginTime="0:0:.2" To="1" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="cp" Storyboard.TargetProperty="Opacity" To="0" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="b2" Storyboard.TargetProperty="Background.RelativeTransform.Angle" By="180" BeginTime="0:0:.2" Duration="0:0:2"/>
<DoubleAnimation Storyboard.TargetName="b12" Storyboard.TargetProperty="Opacity" BeginTime="0:0:2.2" To="1" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="b2" Storyboard.TargetProperty="Opacity" BeginTime="0:0:2.2" To="0" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="b12" Storyboard.TargetProperty="Background.RelativeTransform.Angle" By="180" Duration="0:0:2" BeginTime="0:0:2.2"/>
<!-- Reset -->
<DoubleAnimation Storyboard.TargetName="b1" Storyboard.TargetProperty="Opacity" BeginTime="0:0:4.2" To="0" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="b2" Storyboard.TargetProperty="Opacity" BeginTime="0:0:4.2" To="0" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="b12" Storyboard.TargetProperty="Opacity" BeginTime="0:0:4.2" To="0" Duration="0"/>
<DoubleAnimation Storyboard.TargetName="b2" Storyboard.TargetProperty="Background.RelativeTransform.Angle" To="0" BeginTime="0:0:4.2" Duration="0:0:0"/>
<DoubleAnimation Storyboard.TargetName="b12" Storyboard.TargetProperty="Background.RelativeTransform.Angle" To="0" Duration="0:0:0" BeginTime="0:0:4.2"/>
<DoubleAnimation Storyboard.TargetProperty="Width" To="280" BeginTime="0:0:4.2" Duration="0:0:.2"></DoubleAnimation>
<DoubleAnimation Storyboard.TargetName="b3" Storyboard.TargetProperty="Opacity" BeginTime="0:0:4.2" To="1" Duration="0:0:0"/>
<DoubleAnimation Storyboard.TargetName="b3_" Storyboard.TargetProperty="Opacity" BeginTime="0:0:4.2" To="1" Duration="0:0:.1"/>
<DoubleAnimation Storyboard.TargetName="b3" Storyboard.TargetProperty="Background.RelativeTransform.ScaleX" To="1" Duration="0:0:.2" BeginTime="0:0:4.2"/>
<DoubleAnimation Storyboard.TargetName="b3" Storyboard.TargetProperty="Background.RelativeTransform.ScaleY" To="1" Duration="0:0:.2" BeginTime="0:0:4.2"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="True"/>
<Condition SourceName="b1" Property="Opacity" Value="0"/>
</MultiTrigger.Conditions>
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" To="#FF1ECD97" Duration="0:0:.3">
</ColorAnimation>
<ColorAnimation Storyboard.TargetProperty="Foreground.Color" To="White" Duration="0:0:.3">
</ColorAnimation>
<ThicknessAnimation Storyboard.TargetProperty="BorderThickness" To="0" Duration="0:0:0">
</ThicknessAnimation>
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
<MultiTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetProperty="Background.Color" To="Transparent" Duration="0:0:.3">
</ColorAnimation>
<ColorAnimation Storyboard.TargetProperty="Foreground.Color" To="#FF1ECD97" Duration="0:0:.3">
</ColorAnimation>
<ThicknessAnimation Storyboard.TargetProperty="BorderThickness" To="2" Duration="0:0:0">
</ThicknessAnimation>
</Storyboard>
</BeginStoryboard>
</MultiTrigger.ExitActions>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
Related
I have a color animation for a switch control and to let the background color to be changable I want to bind the Background property of the parent control to the To property of ColorAnimation. I tried a lot but no one worked. How can I do that?
Switch control style:
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="CheckBox">
<Viewbox Stretch="Uniform">
<Border x:Name="layer" Width="35" Height="20" CornerRadius="10,10,10,10"
Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Canvas Canvas.Top="0" Canvas.Left="0">
<Ellipse x:Name="circle" Fill="Gray" Stroke="DarkGray" StrokeThickness="0"
Width="20" Height="20">
<Ellipse.RenderTransform>
<TranslateTransform X="0" Y="0"/>
</Ellipse.RenderTransform>
</Ellipse>
</Canvas>
</Border>
</Viewbox>
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
To="LightGreen"
Duration="0:0:0.3"/>
<DoubleAnimation Storyboard.TargetName="circle"
Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)"
To="15"
Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
Duration="0:0:0.3"
To="{TemplateBinding Background}"/>
<DoubleAnimation Storyboard.TargetName="circle"
Storyboard.TargetProperty="(Ellipse.RenderTransform).(TranslateTransform.X)"
To="0"
Duration="0:0:0.3"/>
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Style>
ColorAnimation part:
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
To="{TemplateBinding Background}"
Duration="0:0:0.3"/>
You can not using binding in color animations,if you do you will be getting this error
"Cannot freeze this Storyboard timeline tree for use across threads", more info on this here:
WPF Animation "Cannot freeze this Storyboard timeline tree for use across threads".
Now if you want a way around, what you can do is make a static resource which represents the background color of your parent control like this:
<Color A="255"
R="100"
G="0"
B="0"
x:Key="resource" />
and then you can assign this resource to your color animation property like this:
<ColorAnimation Storyboard.TargetName="layer"
Storyboard.TargetProperty="Background.Color"
Duration="0:0:0.3"
To="{StaticResource ResourceKey=resource }" />
Hope it helps.
I'm trying to animate a fade-in and fade-out of my ErrorTemplates to not scare my users too much by occurring too quickly. The thing I came up with is the below code (which does not work). I guess the ErrorTemplate does not have access to the attached Validation.HasError property on the control. I also tried a DataTrigger bound to Validation.HasError on self, but that doesn't work either.
<ControlTemplate x:Key="ValidationTemplate">
<ControlTemplate.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation AutoReverse="true"
Duration="0:0:0.5"
RepeatBehavior="3x"
Storyboard.TargetName="border"
Storyboard.TargetProperty="Opacity"
To="0.6" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
</ControlTemplate.Triggers>
<Border x:Name="border"
BorderBrush="Red"
BorderThickness="1"
CornerRadius="2"
Opacity="0.0">
<AdornedElementPlaceholder Margin="-1" />
</Border>
</ControlTemplate>
-- UPDATE
I found out that another less generic but nice way is to restyle controls by implementing ValidationStates visual state group using the visual statemanager. http://msdn.microsoft.com/en-us/library/ms752068%28v=vs.110%29.aspx
You were on the right track with the DataTrigger.
But you need to bind to the adorner's AdornedElement Validation.HasError property with:
<ControlTemplate x:Key="ValidationTemplate">
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=adorner,
Path=AdornedElement.(Validation.HasError)}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation AutoReverse="true"
Duration="0:0:0.5"
RepeatBehavior="3x"
Storyboard.TargetName="border"
Storyboard.TargetProperty="Opacity"
To="0.6" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</ControlTemplate.Triggers>
<Border x:Name="border" BorderBrush="Red" BorderThickness="1" CornerRadius="2"
Opacity="0.0">
<AdornedElementPlaceholder Margin="-1" x:Name="adorner" />
</Border>
</ControlTemplate>
I have the following button style. My question, how can I change the textblock's text colour when the button is not enabled? I tried setting "ButtonTextBlock" value through the storyboard but it cannot seem to access it. Thanks.
<Style TargetType="{x:Type Button}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Viewbox>
<!-- TODO change this textblocks colour depending on IsEnabled -->
<TextBlock x:Name="ButtonTextBlock" HorizontalAlignment="Center" VerticalAlignment="Stretch" Margin="0,2,0,2">
<ContentPresenter Content="{TemplateBinding Content}" >
</ContentPresenter>
</TextBlock>
</Viewbox>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="BorderThickness" Value="1"></Setter>
<Setter Property="BorderBrush" Value="{StaticResource ButtonBorderBrush}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0">
<Border.Background>
<LinearGradientBrush StartPoint="0,0" EndPoint="0,1" >
<GradientStop x:Name="BackgroundGradientStop1" Offset="0" Color="{StaticResource AlbumScrollButtonNormal1}" />
<GradientStop x:Name="BackgroundGradientStop2" Offset="1" Color="{StaticResource AlbumScrollButtonNormal2}" />
</LinearGradientBrush>
</Border.Background>
<ContentPresenter Margin="{TemplateBinding Padding}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<ControlTemplate.Triggers>
<!-- Button Disabled -->
<Trigger Property="IsEnabled" Value="False">
<!-- Disabled -->
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard x:Name="ButtonDisabled">
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonDisabled1}"
Duration="0" />
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonDisabled2}"
Duration="0" />
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<!-- Enabled -->
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard x:Name="ButtonEnabled">
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonNormal1}"
Duration="0" />
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonNormal2}"
Duration="0" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
<!-- Mouse Over and Mouse leave -->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsEnabled" Value="True" />
<Condition Property="IsMouseOver" Value="True" />
</MultiTrigger.Conditions>
<!-- Mouse Over -->
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard x:Name="MouseOverAnimation">
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonMouseOver1}"
Duration="0:0:0:0.1" />
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonMouseOver2}"
Duration="0:0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
<!-- Mouse Leave -->
<MultiTrigger.ExitActions>
<BeginStoryboard>
<Storyboard x:Name="MouseLeaveAnimation" FillBehavior="Stop">
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonNormal1}"
Duration="0:0:0:0.1" />
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonNormal2}"
Duration="0:0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</MultiTrigger.ExitActions>
</MultiTrigger>
<!-- Mouse Press -->
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsEnabled" Value="True" />
<Condition Property="IsPressed" Value="True" />
</MultiTrigger.Conditions>
<!-- Mouse Down -->
<MultiTrigger.EnterActions>
<BeginStoryboard>
<Storyboard x:Name="MouseDownAnimation" >
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonPressed1}"
Duration="0:0:0:0.1" />
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonPressed2}"
Duration="0:0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</MultiTrigger.EnterActions>
<!-- Mouse Up -->
<MultiTrigger.ExitActions>
<BeginStoryboard>
<Storyboard x:Name="MouseUpAnimation" FillBehavior="Stop">
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop1"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonMouseOver1}"
Duration="0:0:0:0.1" />
<ColorAnimation Storyboard.TargetName="BackgroundGradientStop2"
Storyboard.TargetProperty="Color"
To="{StaticResource AlbumScrollButtonMouseOver2}"
Duration="0:0:0:0.1" />
</Storyboard>
</BeginStoryboard>
</MultiTrigger.ExitActions>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
You can animate Foreground.Color of the Button when IsEnabled=False. Also there is no need for ContentTemplate in your Style. You can move ViewBox to ControlTemplate and remove ContentTemplate
<ControlTemplate TargetType="{x:Type Button}">
<Border ...>
<Viewbox>
<ContentPresenter ... />
</Viewbox>
</Border>
<ControlTemplate.Triggers>
<!-- Button Disabled -->
<Trigger Property="IsEnabled" Value="False">
<!-- Disabled -->
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard x:Name="ButtonDisabled">
<ColorAnimation Storyboard.TargetProperty="Foreground.Color" To="Green" Duration="0:0:0.1"/>
</Storyboard>
<!--other animations-->
</BeginStoryboard>
</Trigger.EnterActions>
</Trigger>
<!--other triggers-->
</ControlTemplate.Triggers>
</ControlTemplate>
i want the WPF button behaves in a way on mouse over only i want to show content of button (its an image)
and also on mouseover i want the background as transparent + no border .
Button content is an image and i am giving a padding of 50 px . so mouse is anywhere near to 50px on button i can see the button content.
The xaml i used is
<Button Style="{StaticResource FadeOutButton}" Padding="50" Opacity="0" >
<Image Source="Images\myimage.JPG"></Image>
and
<Style x:Key="FadeOutButton" TargetType="{x:Type Button}">
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<EventTrigger RoutedEvent="Control.MouseEnter">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation Duration="0:0:1" To="1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Control.MouseLeave">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation Duration="0:0:1" To="0" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Style.Triggers>
</Style>
What else i have to add to make the background transparent on mouseover in addition to current effect
Try this
<Window.Resources>
<Style x:Key="FadeOutButton" TargetType="{x:Type Button}">
<Style.Resources>
<Storyboard x:Key="MouseOverAnimation" >
<DoubleAnimation Duration="0:0:1" To="1" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
<Storyboard x:Key="MouseOutAnimation">
<DoubleAnimation Duration="0:0:1" To="0" Storyboard.TargetProperty="Opacity"/>
</Storyboard>
</Style.Resources>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Background="Transparent" >
<ContentPresenter Margin="{TemplateBinding Padding}" HorizontalAlignment="Center" Content="{TemplateBinding Content}" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Transparent"/>
<Trigger.EnterActions>
<BeginStoryboard Storyboard="{StaticResource MouseOverAnimation}" />
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard Storyboard="{StaticResource MouseOutAnimation}" />
</Trigger.ExitActions>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Button Style="{StaticResource FadeOutButton}" Padding="50" Opacity="0" >
<Image Source="Screenshot_3.png"></Image>
</Button>
I am trying to create a designer like Visual Studio.
Suppose I have a Grid.
Inside that I have a TextBox and a TextBlock. For better understanding look at the sample code below:
<Page.Resources>
<Style x:Key="myStyle" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="2" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="DodgerBlue" />
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Border Style="myStyle">
<Grid>
<Border Style="myStyle">
<TextBox ...... />
</Border>
<Border Style="myStyle">
<TextBlock ...... />
</Border>
</Grid>
</Border>
Now when I mouseOver on any of the element I want to get a border around it.
My Problems:
I get a border around grid as well as a border around textblock when mouse cursor is over textblock.
When my mouse cursor goes over empty area of grid the border is not shown.
Requirements :
when mouse cursor goes over textblock, the border around grid should become invisible.
when mouse cursor goes over empty area in grid, the border around grid should become visible.
Please suggest the changes that I should make in the above code to have the required functionality.
Why did you assign myStyle to the outter Border? Just leave that out.
1: I think, your idea with glowing parent border is not good. Because it will be blink whenever user hold mouse over the grid. Perhaps, it will annoy user :)
2: Try to set Grid.Background="Transparent".
Try this
<Grid Background="LightGray" >
<Grid.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Grid_Bd" Duration="0:0:0.1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="1" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Grid_Bd" Duration="0:0:0.1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Grid.Triggers>
<Grid Height="50" Width="50">
<Border x:Name="TextBlock_Bd" Opacity="0" BorderBrush="Blue" BorderThickness="1"/>
<TextBlock Text="Hello !!" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="TextBlock_Bd" Duration="0:0:0.1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="MouseLeave">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="1" To="0" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="TextBlock_Bd" Duration="0:0:0.1"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>
<Border x:Name="Grid_Bd" Opacity="0" BorderBrush="Red" BorderThickness="1"/>
</Grid>