0

我有一个动画到值集的条形图。动画代码看起来像

<UserControl.Resources>
    <Storyboard x:Key="BootUp">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="MainBar">
            <EasingDoubleKeyFrame KeyTime="0" Value="350"/>
            <EasingDoubleKeyFrame KeyTime="0:0:1" Value="{Binding CurrentValue, ElementName=UserControl}"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</UserControl.Resources>

和 C#

int ThisValue=200;
public int CurrentValue
{
    get { return (int)ThisValue; }
    set { 
            ThisValue=(int)value;
            this.MainBar.ToolTip=value.ToString();
            System.Windows.Media.Animation.Storyboard storyBoard = (System.Windows.Media.Animation.Storyboard)FindResource("BootUp");
            storyBoard.Begin(this);
        }

}

我认为这应该有效,但每次它返回

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=UserControl'. BindingExpression:Path=CurrentValue; DataItem=null; target element is 'EasingDoubleKeyFrame' (HashCode=27594380); target property is 'Value' (type 'Double')

到底是怎么回事?

4

1 回答 1

2

如果您更改绑定找到 UserControl 的方式,它应该可以工作。

替换这个:

Value="{Binding CurrentValue, ElementName=UserControl}"

..有了这个:

Value="{Binding CurrentValue, RelativeSource={RelativeSource AncestorType=UserControl}}
于 2012-11-14T19:23:53.627 回答