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>
Related
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
});
I have added an effect to Viewbox as below:
<Viewbox Height="40" Width="40" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,12.5,65,0" >
<Viewbox.Effect>
<effects:RippleEffect Magnitude="0" Frequency="0" />
</Viewbox.Effect>
<Grid>
<Rectangle x:Name="MinimizeRect" RadiusX="7" RadiusY="7" StrokeThickness="2" Stroke="White" Fill="Transparent"/>
<Image Source="Images/Minimize.png" Margin="5"/>
</Grid>
<Viewbox.Triggers>
<EventTrigger RoutedEvent="Mouse.MouseEnter" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Effect).Magnitude" To="0.1" Duration="0:0:0.3" AutoReverse="True" RepeatBehavior="Forever" />
<DoubleAnimation Storyboard.TargetProperty="(Effect).Frequency" To="40" Duration="0:0:0.3" AutoReverse="True" RepeatBehavior="Forever"/>
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" To="DodgerBlue" Duration="0:0:0.3" Storyboard.TargetName="MinimizeRect"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Mouse.MouseLeave" >
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Effect).Magnitude" To="0" Duration="0:0:0.3" />
<DoubleAnimation Storyboard.TargetProperty="(Effect).Frequency" To="0" Duration="0:0:0.3" />
<ColorAnimation Storyboard.TargetProperty="(Rectangle.Fill).(SolidColorBrush.Color)" To="Transparent" Duration="0:0:0.3" Storyboard.TargetName="MinimizeRect"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Viewbox.Triggers>
</Viewbox>
But my image and Stroke of rectangle does not remain white. They are grayed. What might be the problem here?
Here is the image.
In the above image Ripple effect is added to Minimize button. Any effect is not added to close button.
Try setting the RenderOptions.BitmapScalingMode to HighQuality like so:
<Image Source="Images/Minimize.png" Margin="5"
RenderOptions.BitmapScalingMode="HighQuality" />
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" ... />
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.
I'm currently working on a Surface application where I need to call two different animations when a button is tapped.
How exactly should I be doing this? I'd like to do it declaratively if it's possible. Should I be using MultiTriggers for this, or?
Thanks in advance!
You can do this with an EventTrigger.
You can define the trigger in a FrameworkElement.Triggers property of any container of both the button and the animation targets.
<StackPanel
Orientation="Horizontal">
<StackPanel.Triggers>
<EventTrigger
SourceName="TheButton"
RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<ColorAnimation
Storyboard.TargetName="LimeRect"
Storyboard.TargetProperty="Fill.Color"
To="Red" />
<ColorAnimation
Storyboard.TargetName="RedRect"
Storyboard.TargetProperty="Fill.Color"
To="Lime" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</StackPanel.Triggers>
<Button
x:Name="TheButton"
Content="Play" />
<Rectangle
x:Name="LimeRect"
Fill="Lime"
Width="50"
Height="50" />
<Rectangle
x:Name="RedRect"
Fill="Red"
Width="50"
Height="50" />
</StackPanel>
If there is a relative path to your targets, you can use Storyboard.Target="{Binding PathToTarget}" in place of Storyboard.TargetName="TargetName".
EDIT: (see comments)
If you are animating the button itself, you can put the triggers right in the button and you don't need any target names.
Example - Animating the size of a ToggleButton:
<ToggleButton
Content="Toggle"
Width="50"
Height="50">
<ToggleButton.Triggers>
<EventTrigger
RoutedEvent="ToggleButton.Checked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Width"
To="100" />
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Height"
To="100" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger
RoutedEvent="ToggleButton.Unchecked">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Width"
To="50" />
<DoubleAnimation
Duration="00:00:00.2"
Storyboard.TargetProperty="Height"
To="50" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToggleButton.Triggers>
</ToggleButton>