为什么 XAML 动画的属性值需要用圆括号括起来? [英] Why do you need to have round brackets around attribute values for XAML animations?

查看:30
本文介绍了为什么 XAML 动画的属性值需要用圆括号括起来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这已经困扰我很长时间了,我似乎找不到一个很好的解释.此标记中圆括号的用途是什么?它是用于转换的 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)"

推荐答案

这是用于指定类型限定 DependencyProperty 的语法.它是必需的,因为 Storyboard.TargetProperty 附加属性可以附加到任何 DependencyObject.这意味着 XAML 解析器不知道如何解析属性,除非它们是完全限定的.

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>

如果从 Binding 中删除括号,则会出现绑定错误(因为 Border 元素上没有 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).

这篇关于为什么 XAML 动画的属性值需要用圆括号括起来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆