Xaml Doubleanimation targetproperty crashes - wpf

I would like to animate my Label inside of canvas from canvas right to canvas left + size of label, infinitely. Which means my label comes from the right side and goes to the left till the end and then again repeats.
This is my xaml:
<Canvas Margin="0, 0, 0, 20" Name="CanMain2" Height="30" Width="350" >
<Label x:Name="LabelNameSong" Content="Hello" >
<Label.Resources>
<Storyboard x:Key="Scroll">
<DoubleAnimation To="{Binding ActualWidth, ElementName=LabelNameSong}" Duration="00:00:10"
Storyboard.TargetProperty="(Canvas.Right)"
Storyboard.TargetName="LabelNameSong"
RepeatBehavior="Forever"/>
</Storyboard>
</Label.Resources>
<Label.Triggers>
<EventTrigger RoutedEvent="Label.Loaded">
<BeginStoryboard Storyboard="{StaticResource Scroll}" />
</EventTrigger>
<EventTrigger RoutedEvent="Label.SizeChanged">
<BeginStoryboard Storyboard="{StaticResource Scroll}" />
</EventTrigger>
</Label.Triggers>
</Label>
</Canvas>
As soon as I launch app it crashes, and debugger says:
Cannot animate the 'Right' property on a
'System.Windows.Controls.Label' using a
'System.Windows.Media.Animation.DoubleAnimation'. For details see the
inner exception.
I am new to xaml and can't seem to make this work.

This is because Canvas.Right is an AttachedProperty but you have not attached the property to your Label
If you add the AttachedProperty to your Label it will then let you animate the value as the Property will be registered(attached) to the Label
Example:
<Canvas Margin="0, 0, 0, 20" Name="CanMain2" Height="30" Width="350" >
<Label x:Name="LabelNameSong" Content="Hello" Canvas.Right="0" >
<Label.Resources>
<Storyboard x:Key="Scroll">
<DoubleAnimation To="{Binding ActualWidth, ElementName=LabelNameSong}" Duration="00:00:10"
Storyboard.TargetProperty="(Canvas.Right)"
Storyboard.TargetName="LabelNameSong"
RepeatBehavior="Forever"/>
</Storyboard>
</Label.Resources>
<Label.Triggers>
<EventTrigger RoutedEvent="Label.Loaded">
<BeginStoryboard Storyboard="{StaticResource Scroll}" />
</EventTrigger>
<EventTrigger RoutedEvent="Label.SizeChanged">
<BeginStoryboard Storyboard="{StaticResource Scroll}" />
</EventTrigger>
</Label.Triggers>
</Label>
</Canvas>

Related

Use relative binding to bind prop Window.ActualHeight to DoubleAnimation.To prop

I try use relative binding and bind Window.ActualHeight property to DoubleAnimation.To property.
Relative binding doesn’t work but if I use binding by ElementName it work.
NOT WORK:
<Window xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Sample.Window"
x:Name="Shell"
Title="Sample"
Width = "525"
Height = "350">
<Canvas Height="400">
<Ellipse Canvas.Left="80"
Canvas.Top="0"
Width="30"
Height="30"
Fill="LimeGreen">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
Duration="0:0:5"
RepeatBehavior="Forever"
AutoReverse="True"
To="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}, Mode=FindAncestor}, Path=ActualHeight}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Canvas>
</Window>
THIS WORK:
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
Duration="0:0:5"
RepeatBehavior="Forever"
AutoReverse="True"
To="{Binding ElementName=Shell, Path=ActualHeight}"/>
It is possible use relative binding to Window.ActualHeight property ?
EDITED:
I tested again on routed event Ellipse.Loaded animation not work but if I changed routed event to MouseEnter animation work. I posted all project.
sample download
Thank
Strangely if any frameworkElement traverses Visual Tree till ancestral window, in that case DoubleAnimation too is able to find ancestral window but if no element is referring to ancestral window, DoubleAnimation is not able to traverse up the Visual tree.
In your sample in case i simply traverse till window outside of animation, it works. Test this sample (traverse tree using Tag property of canvas)-
<Canvas Height="400"
Tag="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType=Window}}"> <-HERE
<Ellipse Canvas.Left="80"
Canvas.Top="0"
Width="30"
Height="30"
Fill="LimeGreen">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetProperty="(Canvas.Top)"
Duration="0:0:5"
RepeatBehavior="Forever"
AutoReverse="True"
To="{Binding RelativeSource={RelativeSource
AncestorType={x:Type Window},
Mode=FindAncestor},
Path=ActualHeight}" />
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</Canvas>

WPF event trigger not working

I'm new in programming (changing area, leaving Design) and pretty interested in WPF.
So I'm studying and doing some codes.
In this one, I'm trying to make an "invisible" grid (the gridNovoCliente, opacity = 0) to show up at a button click event (opacity = 1), with all its elements.
I want the grid/elements invisible until the user clicks the button.
But it's not working.
I've been Googling it for 3 days before decided to post here.
I get the error 'Set property 'System.Windows.Media.Animation.Storyboard.Target' threw an exception.' at the EventTrigger line.
There will be more buttons once I get this one working, so I created a style.
Thanks in advance!
Here's the code.
<Grid>
<Button Style="{StaticResource templateButton}" Name="novoCliente" Content="Novo
Cliente" Margin="20,41,0,598" TextBlock.TextAlignment="Center">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.Target="gridNovoCliente"
Storyboard.TargetProperty="Opacity"
From="0"
To="1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Grid Height="705" HorizontalAlignment="Left" Margin="131,12,0,0" Name="gridNovoCliente" VerticalAlignment="Top" Width="859" Background="#FF6FAA6F" Opacity="0">
<TextBox Height="70" HorizontalAlignment="Left" Margin="270,250,0,0" Name="textBox1" VerticalAlignment="Top" Width="358" />
</Grid>
</Grid>
<Canvas>
<Button Name="novoCliente" Content="Novo
Cliente" Margin="20,41,0,598" TextBlock.TextAlignment="Center">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click" >
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="gridNovoCliente"
Storyboard.TargetProperty="Opacity"
From="0"
To="1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
</Button>
<Grid Height="705" HorizontalAlignment="Left" Margin="131,12,0,0" Name="gridNovoCliente" VerticalAlignment="Top" Width="859" Background="#FF6FAA6F" Opacity="0">
<TextBox Height="70" HorizontalAlignment="Left" Margin="270,250,0,0" Name="textBox1" VerticalAlignment="Top" Width="358" />
</Grid>
</Canvas>
It's very simple, please try above xaml, it's works for me. Thanks

XamlParseException occured - animating an ellipse

I have the following code to animate the width of the ellipse forever until the window is closed. However, it throws XamlParseExceptionError. Could someone point out to me where I made the mistake?
`
</TextBlock>
<TextBlock x:Uid="ProductName" Localization.Attributes="$Content(Readable Unmodifiable)">
<TextBlock.ToolTip>
<TextBlock x:Uid="ProductNameTip" Localization.Attributes="$Content(ToolTip Readable Modifiable)">
A photo editor that will make everyone look beautiful
</TextBlock>
</TextBlock.ToolTip>
Amazing Photo Editor
</TextBlock>
<Ellipse Name="Circle"
Width="100"
Height="100"
Fill="Red">
<Ellipse.Triggers>
<EventTrigger RoutedEvent="Ellipse.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.Target="Circle"
Storyboard.TargetProperty="Width"
From="1"
To="100"
Duration="0:0:5"
AutoReverse="True"
RepeatBehavior="Forever">
</DoubleAnimation>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
</StackPanel>
</Grid>
`
Use Storyboard.TargetName Attached Property instead of Storyboard.Target.
<DoubleAnimation Storyboard.TargetName="Circle"

Can not find the EventTrigger.SourceName

<Canvas>
<Button Name="b1" Content="Button"/>
<Rectangle Name="Mr1" Fill="Black">
<Rectangle.Triggers>
<EventTrigger SourceName="b1" RoutedEvent="Button.Click">
<BeginStoryboard>
<code.../>
</BeginStoryboard>
</EventTrigger>
</Rectangle.Triggers>
</Rectangle>
</Canvas>
Error: can not find the b1.
Please help me, thank you.
I was just facing this same problem.. posting the solution for anyone else interested.
<Canvas>
<Canvas.Resources>
<Storyboard x:Key="MyAwesomeAnimation" Storyboard.TargetName="Mr1" Storyboard.TargetProperty="Height">
<!-- Some Awesome animation here..-->
</Storyboard>
</Canvas.Resources>
<Button Name="b1" Content="Button">
<Button.Triggers>
<!--The key is to define the click trigger on the button. Why should the rectangle know about the button?-->
<EventTrigger RoutedEvent="Button.Click">
<BeginStoryboard Storyboard="{StaticResource MyAwesomeAnimation}"></BeginStoryboard>
</EventTrigger>
</Button.Triggers>
</Button>
<Rectangle Name="Mr1" Fill="Black" />
</Canvas>
You can set this property to the name of any element within the scope of where the trigger collection (the collection that this Trigger is part of) is applied. This is typically a named element that is within the template that contains this Trigger.

Creating button content with centered text and animated border

The following XAML code creates a button with a border as its content. The border has a TextBlock as it's child. When you CLICK on the button, the border width gradually grows.
Since the text block is inside the border it is not seen until you click the button.
Now my requirements are:
There should be a text displayed on the button even before you click it.
Even after you click the button, only the border should animate while the button text remains in its center position.
<Button Name="button5" Width="100" Margin="10" HorizontalContentAlignment="Left">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="myBorder" Storyboard.TargetProperty="Width" From="0" To="94" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
<Button.Content>
<Border Background="Gray" Width="0" Name="myBorder">
<TextBlock>Search</TextBlock>
</Border>
</Button.Content>
</Button>
I will appreciate if anyone is able to provide answers with working XAML code.
I've modified your XAML only slightly:
<Button Name="button5" Width="100" Margin="10" HorizontalContentAlignment="Left">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.Click">
<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="myRectangle" Storyboard.TargetProperty="Width" From="0" To="94" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</Button.Triggers>
<Button.Content>
<Grid>
<Rectangle x:Name="myRectangle" Fill="Gray" Width="0" HorizontalAlignment="Left" />
<TextBlock Text="Search" />
</Grid>
</Button.Content>
</Button>
For the Button's content, you could use a Grid which contains your Border and the TextBlock. Since no columns or rows are specified, the TextBlock and the Border are drawn on top of each other. It situations like this where the Border does not have content, I prefer to use a Rectangle instead since it is bit lighter. Hope this helps!

Resources