Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do you need to have round brackets around attribute values for XAML animations?

Tags:

wpf

xaml

This has been bugging me for a long time now and I can't seem to find a good explanation for it. What is the purpose of the round brackets in this markup? Is it a XAML shortcut for casting? Why does it only seem to be used for animations?

Storyboard.TargetProperty="(TextBlock.RenderTransform).(RotateTransform.Angle)"
like image 760
Matt Avatar asked Sep 17 '10 17:09

Matt


1 Answers

This is syntax for specifying a Type qualified DependencyProperty. It is required, because the Storyboard.TargetProperty attached property can be attached to any DependencyObject. That means the XAML parser won't know how to resolve the properties unless they are fully qualified.

This syntax is also used for things like binding to attached properties. Here is a contrived example to demonstrate 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 parenthesis from the Binding, you'll get a binding error (because there is no Grid property on the Border element).

like image 183
Abe Heidebrecht Avatar answered Nov 15 '22 14:11

Abe Heidebrecht