Why do you need parentheses around attribute values ​​for XAML animations?

This has long distorted me, and I cannot find a good explanation for this. What is the purpose of parentheses in this markup? Is this a XAML shortcut for casting? Why does it only seem to be used for animation?

Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)" 
+6
wpf xaml
source share
2 answers

This is the syntax for specifying the type DependencyProperty . This is necessary because the attached property Storyboard.TargetProperty can be attached to any DependencyObject . This means that the XAML parser will not know how to resolve properties if they do not fully meet the requirements.

This syntax is also used for things like binding to attached properties. Here is an example demonstrating this:

 <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Border x:Name="Foo" Background="Blue" Grid.Row="10" /> <Border x:Name="Bar" Background="Red" Height="{Binding (Grid.Row), ElementName=Foo}" /> </Grid> 

If you remove the bracket from Binding , you will get a binding error (because there is no Grid property in the Border element).

+4
source share

It is used not only for animation (it is important to remember the presence of validation) - these are just static calls or casts, respectively. Basically the code above translates to (in pseudo-code):

 ((RotateTransform)TextBlock.GetRenderTransform((TextBlock) element)).Angle = newValue; 

where the element is the element it is acting on, and newValue are the animation settings properties.

0
source share

All Articles