Quantcast
Viewing all articles
Browse latest Browse all 9

Button Animation

How about using a control that derives from ToggleButton instead of plain Button? i.e. CheckBox. Then restyle the CheckBox so that it looks like a regular Button.

Seems to me like you are talking about a two state button? If so then you can trigger your animation using the Checked property. I have knocked up an example below:

Code Snippet


<Window

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Classic"

x:Class="CheckBoxAnim.Window1"

x:Name="Window"

Title="Window1"

Width="640"Height="480">

<Window.Resources>


<Stylex:Key="ButtonFocusVisual">

<SetterProperty="Control.Template">

<Setter.Value>

<ControlTemplate>

<Border>

<RectangleSnapsToDevicePixels="True"Stroke="Black"StrokeDashArray="1 2"StrokeThickness="1"Margin="4"/>

</Border>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>


<Stylex:Key="CheckBoxStyleCopiedFromButton"TargetType="{x:Type CheckBox}">

<SetterProperty="FocusVisualStyle"Value="{StaticResource ButtonFocusVisual}"/>

<SetterProperty="Background"Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>

<SetterProperty="Foreground"Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>

<SetterProperty="BorderBrush"Value="{x:Static Microsoft_Windows_Themes:ClassicBorderDecorator.ClassicBorderBrush}"/>

<SetterProperty="BorderThickness"Value="3"/>

<SetterProperty="HorizontalContentAlignment"Value="Center"/>

<SetterProperty="VerticalContentAlignment"Value="Center"/>

<SetterProperty="Padding"Value="0,0,1,1"/>

<SetterProperty="Template">

<Setter.Value>


<ControlTemplateTargetType="{x:Type CheckBox}">

<Microsoft_Windows_Themes:ClassicBorderDecoratorSnapsToDevicePixels="true"x:Name="ContentContainer"Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderStyle="Raised"BorderThickness="3">

<ContentPresenterSnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"Margin="{TemplateBinding Padding}"VerticalAlignment="{TemplateBinding VerticalContentAlignment}"RecognizesAccessKey="True"/>

</Microsoft_Windows_Themes:ClassicBorderDecorator>

<ControlTemplate.Resources>

</ControlTemplate.Resources>

<ControlTemplate.Triggers>

<TriggerProperty="IsKeyboardFocused"Value="true">

<SetterProperty="BorderStyle"TargetName="ContentContainer"Value="RaisedFocused"/>

</Trigger>

<TriggerProperty="IsPressed"Value="true">

<SetterProperty="BorderStyle"TargetName="ContentContainer"Value="RaisedPressed"/>

</Trigger>

<TriggerProperty="IsChecked"Value="True">

<Trigger.EnterActions>

<BeginStoryboard>

<Storyboard>

<DoubleAnimationStoryboard.TargetProperty="(FrameworkElement.Width)"By="40"Duration="0:0:1" />

<DoubleAnimationStoryboard.TargetProperty="(FrameworkElement.Height)"By="20"Duration="0:0:1" />

</Storyboard>

</BeginStoryboard>

</Trigger.EnterActions>

<Trigger.ExitActions>

<BeginStoryboard>

<Storyboard>

<DoubleAnimationStoryboard.TargetProperty="(FrameworkElement.Width)"To="90"Duration="0:0:1" />

<DoubleAnimationStoryboard.TargetProperty="(FrameworkElement.Height)"To="20"Duration="0:0:1" />

</Storyboard>

</BeginStoryboard>

</Trigger.ExitActions>

</Trigger>

<TriggerProperty="IsEnabled"Value="false">

<SetterProperty="Foreground"Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>

</Trigger>

</ControlTemplate.Triggers>

</ControlTemplate>


</Setter.Value>

</Setter>

</Style>


</Window.Resources>


<Gridx:Name="LayoutRoot">

<StackPanel>

<CheckBoxHorizontalAlignment="Left"Style="{DynamicResource CheckBoxStyleCopiedFromButton}"VerticalAlignment="Top"Content="I'm a CheckBox"x:Name="checkBox"Width="90"Height="20"/>

<ButtonHorizontalAlignment="Left"VerticalAlignment="Top"Content="I'm a Button"/>

</StackPanel>

</Grid>


</Window


The only bit I cant figure out is how to make the sizes a bit more elegant, so it knows how to go back to the original size (perhaps even if it was Auto sized!)...

Viewing all articles
Browse latest Browse all 9

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>