Button Bounce on PropertyChange - wpf

I have a simple button (3 of them - all same except the content binding which is just an integer.
<Button Width="24" Height="24" Style="{StaticResource CircleButton}"
Background="#CC2B2B" Foreground="#FFFFFF"
Content="{Binding PinnedItemsCount,
FallbackValue=0,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
NotifyOnTargetUpdated=True}"
Name="ui_btnPinnedCount" />
How do I make the button boune when the data changes?
The underlying content implements INotifyPropertyChanged and the data bindings are all working, just need the button to bounce 3 times once the data changes....
Thanks!

Use animations with a DataTrigger
http://msdn.microsoft.com/en-us/library/system.windows.media.animation.bounceease.aspx
How to: Trigger an Animation When Data Changes
<Rectangle Name="myRectangle" Width="200" Height="30" Fill="Blue">
<Rectangle.Triggers>
<EventTrigger RoutedEvent="Rectangle.MouseDown">
<BeginStoryboard>
<Storyboard>
<Storyboard x:Name="myStoryboard">
<DoubleAnimation From="30" To="200" Duration="00:00:3"
Storyboard.TargetName="myRectangle"
Storyboard.TargetProperty="Height">
<DoubleAnimation.EasingFunction>
<BounceEase Bounces="2" EasingMode="EaseOut"
Bounciness="2" />
</DoubleAnimation.EasingFunction>
</DoubleAnimation>
</Storyboard>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>

This makes it fade in and out, replace with any sotryboard animation I imagine.
<Button Width="24" Height="24" Style="{StaticResource CircleButton}" Background="#CC2B2B" Foreground="#FFFFFF"
Content="{Binding PinnedCount, FallbackValue=0, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}"
Name="ui_btnPinnedCount">
<Button.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard AutoReverse="True">
<DoubleAnimation Storyboard.TargetName="ui_btnPinnedCount" Storyboard.TargetProperty="Opacity" To=".1" Duration="0:0:.5" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>

Related

Start a storyboard from code-behind (xaml.cs), not from view model MVVM

In WPF I have set below style for a border:
<Style TargetType="Border" x:Key="BorderBlinking">
<Style.Triggers>
<DataTrigger Binding="{Binding PopupBlinking}" Value="True">
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="0" AutoReverse="True" Duration="0:0:0.5" SpeedRatio="3" RepeatBehavior="3x" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
<DataTrigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="1" AutoReverse="True" Duration="0:0:0.5" />
</Storyboard>
</BeginStoryboard>
</DataTrigger.ExitActions>
</DataTrigger>
</Style.Triggers>
</Style>
and I attach to the border like this:
<Border Grid.Row="2" x:Name="popup"
Style="{StaticResource BorderBlinking}"
CornerRadius="10,10,0,0" Height="25" Margin="0"
HorizontalAlignment="Center" Width="Auto"
VerticalAlignment="Center"
BorderBrush="DarkBlue" BorderThickness="1"
Background="AntiqueWhite">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Source="Common.Images;component/Images/Info.png" Height="20" Width="20" Stretch="Fill"/>
<TextBlock Margin="5" VerticalAlignment="Center" HorizontalAlignment="Left"
Background="Transparent" FontSize="12"><Run Text="this is a custom popup"/></TextBlock>
</StackPanel>
</Border>
Then from my code behind (not view model) I want to start the storyboard. I know how to start it from view model through a property "PopupBlinking" (as above in the example) bound to the datatrigger but now I need to know how to start it from code-behind (not view model).
I have modified code above and done below:
<Storyboard x:Key="Blink" >
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="0" AutoReverse="True" Duration="0:0:0.5" SpeedRatio="3" RepeatBehavior="3x" />
<DoubleAnimation Storyboard.TargetProperty="Opacity"
To="1" AutoReverse="True" Duration="0:0:0.5" />
</Storyboard>
and from code-behind:
Storyboard sb = Resources["Blink"] as Storyboard;
sb.Begin(this.popup);
Is this the correct way to do it?
You could directly start an animation like this:
popup.BeginAnimation(UIElement.OpacityProperty,
new DoubleAnimation
{
To = 0,
Duration = TimeSpan.FromSeconds(0.5),
AutoReverse = true,
RepeatBehavior = RepeatBehavior.Forever
});

How to add animation to margin property of TextBlock in WPF

I want to make TextBlock like this
But,
aTextBlock.BeginAnimation(Button.MarginProperty, myDoubleAnimation);
get this error
'System.Windows.Media.Animation.DoubleAnimation' cannot be used to animate the 'Margin' property of type 'System.Windows.Thickness'.
I have test in Xaml and get same error:
<Border CornerRadius="8" Background="Red" Margin="352,173,214,368">
<TextBlock x:Name="TestB"
Text="{Binding ElementName=MTxt,Path=Text,NotifyOnSourceUpdated=True}"
Margin="0,0,0,0"
HorizontalAlignment="Center"
Foreground="White"
FontWeight="Bold"
FontSize="16">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Storyboard.TargetName="TestB"
Storyboard.TargetProperty="(TextBlock.Margin)"
To="2"
Duration="0:0:1" AutoReverse="True"
RepeatBehavior="2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Border>
What is your idea?
You need to use ThicknessAnimation for this. DoubleAnimation is for properties of Double type.
<TextBlock.Triggers>
<EventTrigger RoutedEvent="TextBlock.Loaded">
<BeginStoryboard>
<Storyboard>
<ThicknessAnimation
Storyboard.TargetName="TestB"
Storyboard.TargetProperty="(TextBlock.Margin)"
To="2"
Duration="0:0:1" AutoReverse="True"
RepeatBehavior="2" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>

using transformgroup in wpf gives an error

i was searching for combining multiple transform in an object and i found out how but the problem is when i try to put transformgroup, it gives an error "Cannot resolve all property references in the property path 'RenderTransform.ScaleX'. Verify that applicable objects support the properties."
here's my transformation code which i copied from the net ("thanks to those who made this code")
<Window.Resources>
<Storyboard x:Key="expandStoryboard">
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
To="1.3" Duration="0:0:0.2" />
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
To="1.3" Duration="0:0:0.2" />
</Storyboard>
<Storyboard x:Key="shrinkStoryboard">
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX"
To="1" Duration="0:0:0.2" />
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleY"
To="1" Duration="0:0:0.2" />
</Storyboard>
<Storyboard x:Key="shakeStoryBoard">
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Angle"
From="-5" To="5" Duration="0:0:0.05"
AutoReverse="True"
RepeatBehavior="3x"
FillBehavior="Stop" />
</Storyboard>
and this is my object (button)
<Button RenderTransformOrigin="0.5,0.5" Background="Transparent" Focusable="False" BorderBrush="Transparent" Height="220" HorizontalAlignment="Left" Margin="591,213,0,0" Name="cmdsettings" VerticalAlignment="Top" Width="189">
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center" Width="217" Height="220">
<Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="Images/settings.png" Height="192" Width="204" />
<TextBlock VerticalAlignment="center" TextAlignment="center" FontSize="16" Width="123" Foreground="white" FontWeight="Bold" Height="20">Settings</TextBlock>
</StackPanel>
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard Storyboard="{StaticResource expandStoryboard}" />
</EventTrigger>
<EventTrigger RoutedEvent="Button.MouseLeave">
<BeginStoryboard Storyboard="{StaticResource shrinkStoryboard}" />
</EventTrigger>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard Storyboard="{StaticResource shakeStoryBoard}" />
</EventTrigger>
</Button.Triggers>
<Button.RenderTransform >
<TransformGroup>
<ScaleTransform ScaleX="1" ScaleY="1"/>
<RotateTransform />
</TransformGroup>
</Button.RenderTransform>
</Button>
Since Button.RenderTransform contains a TransformGroup, you would have to access the contained Transforms by its Children property:
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[0].ScaleX" ... />
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[0].ScaleY" ... />
<DoubleAnimation Storyboard.TargetProperty="RenderTransform.Children[1].Angle" ... />

How to grow a listbox from 0 to the size of the grid in XAML

I want the value To="200.0" will be equal to the grid size automaticaly
<ListBox HorizontalAlignment="Stretch" Grid.Column="1"
Margin="12,90,12,12"
Name="listBox1"
Opacity="0.6"
VerticalAlignment="Stretch" BorderThickness="0.5" BorderBrush="White">
<ListBox.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="listBox1"
Storyboard.TargetProperty="Width"
From=" 0.0" To="200.0" />
<DoubleAnimation Storyboard.TargetName="listBox1"
Storyboard.TargetProperty="Height"
From="0.0" To="200.0" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ListBox.Triggers>
</ListBox>
try something like this:
<Grid Name="test">
<ListBox HorizontalAlignment="Stretch" Name="listBox1"
VerticalAlignment="Stretch" BorderThickness="0.5" BorderBrush="Black">
<ListBox.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="listBox1"
Storyboard.TargetProperty="Width"
From="0.0" To="{Binding ElementName=test,Path=ActualWidth}" />
<DoubleAnimation Storyboard.TargetName="listBox1"
Storyboard.TargetProperty="Height"
From="0.0" To="{Binding ElementName=test,Path=ActualHeight}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ListBox.Triggers>
</ListBox>
</Grid>
I used ElementName binding instead of a RelativeSource binding because I can't make RelativeSource to work. I guess because the story board is not part of the same visual/control tree as the listbox.

ActualWidth as value of From WPF animation

Why can't I refer to ActualWidth as a value from, which I was able to use in code.
XAML:
<StackPanel>
<Button x:Name="cmdVibrate" HorizontalAlignment="Center">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="{Binding RelativeSource={RelativeSource Mode=Self},Path=ActualWidth}" By="200" AutoReverse="True" Duration="0:0:1" Storyboard.TargetProperty="Width"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
Mouse over me to shake
</Button>
</StackPanel>
</Window>
RelativeSource={RelativeSource Mode=Self} will probably be refering to the DoubleAnimation (which doesn't have an ActualWidth property). Try to find the ancestor Button instead
From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}},
Path=ActualWidth}"
Xaml
<StackPanel>
<Button x:Name="cmdVibrate" HorizontalAlignment="Center">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.MouseEnter">
<BeginStoryboard>
<Storyboard >
<DoubleAnimation From="{Binding RelativeSource={RelativeSource AncestorType={x:Type Button}}, Path=ActualWidth}"
By="200"
AutoReverse="True"
Duration="0:0:1"
Storyboard.TargetProperty="Width"></DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
Mouse over me to shake
</Button>
</StackPanel>

Resources