【发布时间】:2017-07-18 04:05:45
【问题描述】:
我在 Stackoverflow 上搜索过这个问题,但我认为其他帖子没有涵盖这个问题。
在我的自定义控件中,我使用的是可视状态管理器。在 Visual State Manager 内部有一个动画,它可以为元素的高度设置动画。当我尝试绑定到控件属性时,我在启动时收到以下错误:
附加信息:无法通过引用“RelativeSource FindAncestor, AncestorType=MyNameSpace.MyControl, AncestorLevel='1'”找到绑定源。绑定表达式:路径=实际高度;数据项=空;目标元素是“DoubleAnimation”(HashCode=562002);目标属性是'To'(类型'Nullable`1')
我的控件如下所示:
<Style TargetType="{x:Type local:MyControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:MyControl}">
<Grid x:Name="RootGrid" >
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CheckStates">
<VisualState x:Name="Checked">
<Storyboard>
<DoubleAnimation Storyboard.TargetName="someElement"
Storyboard.TargetProperty="Height"
From="0"
To="{Binding RelativeSource={RelativeSource AncestorType=local:MyControl}, Path=CustomControlProperty}"
Duration="0:0:.7" />
...
我尝试了所有的 Bindings 方式,但似乎 Animations 总是将自己作为 Scope。
再次感谢您的帮助。
【问题讨论】:
-
是的,从那里到父控件不会有可视树关系,所以这会失败。但是
RootGrid将具有与模板化父级相同的ActualHeight,不是吗?我会尝试{Binding ActualHeight, ElementName=RootGrid}。是的,这有点像kludge。如果它甚至有效。 -
我犯了一个错误。我不想绑定到 Height 属性。如果是这种情况,你是对的。我想绑定到自定义控件中定义的自定义属性。