WPF资源故事板绑定

时间:2013-02-18 10:46:27

标签: c# wpf animation binding storyboard

我有以下动画,就像魅力一样:

<Window.Resources>
    <Namespace:MathConverter Core:Key="MathConverter"/>
    <Storyboard Core:Key="MyKey" Completed="OnCompleted">
        <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" BeginTime="00:00:00.0" By="135" Duration="00:00:0.2"/>
        <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)" BeginTime="00:00:00.2" Duration="00:00:00.1" To="0">
            <DoubleAnimation.EasingFunction>
                <QuinticEase EasingMode="EaseInOut"/>
            </DoubleAnimation.EasingFunction>
        </DoubleAnimation>
    </Storyboard>
</Window.Resources>

现在......我的布局是根据一些特定的基本值动态构建的。而且我希望我的动画表现得一样。问题是,如果我修改我的声明如下(MultiBinding Converter的返回值是绝对正确的,我仔细检查它)它只是停止正常工作:

<Storyboard Core:Key="MyKey" Completed="OnCompleted">
    <DoubleAnimation Storyboard.TargetProperty="(Canvas.Left)" BeginTime="00:00:00.0" Duration="00:00:0.2">
        <DoubleAnimation.By>
            <MultiBinding Converter="{StaticResource MathConverter}" ConverterParameter="((x - y) / 2) + z + w">
                <Binding Source="{Core:Static Namespace:MyClass.RealHeight}"/>
                <Binding Source="{Core:Static Namespace:MyClass.RealWidth}"/>
                <Binding Source="{Core:Static Namespace:MyClass.MarginInner}"/>
                <Binding Source="{Core:Static Namespace:MyClass.RealWidth}"/>
            </MultiBinding>
        </DoubleAnimation.By>
    </DoubleAnimation>
    <DoubleAnimation Storyboard.TargetProperty="(FrameworkElement.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)" BeginTime="00:00:00.2" Duration="00:00:00.1" To="0">
        <DoubleAnimation.EasingFunction>
            <QuinticEase EasingMode="EaseInOut"/>
        </DoubleAnimation.EasingFunction>
    </DoubleAnimation>
</Storyboard>

“By”值在运行时调试时为null。为什么绑定不能在这里工作?有没有解决方法?在代码中手动设置该值当然有效......但是...... [/ p>

m_MyAnimation = (Storyboard)Resources["MyKey"];
((DoubleAnimation)m_MyAnimation.Children[0]).By = ((MyClass.RealHeight - MyClass.RealWidth) / 2D) + MyClass.MarginInner + MyClass.RealWidth;

1 个答案:

答案 0 :(得分:0)

问题是我的Converter处理所有返回值的方式:

public Object Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture)
{
    try
    {
        String text = parameter.ToString();
        IExpression expression = null;

        if (!m_Expressions.TryGetValue(text, out expression))
            m_Expressions[text] = expression = m_Parser.Parse(text);

        Decimal result = expression.Eval(values);

        if (targetType == typeof(Decimal))
            return result;

        if (targetType == typeof(Double))
            return (Double)result;

        if (targetType == typeof(Int32))
            return (Int32)result;

        if (targetType == typeof(Int64))
            return (Int64)result;

        if (targetType == typeof(String))
            return result.ToString();
    }
    catch { }

    return DependencyProperty.UnsetValue;
}

那种动画值是可以为空的...所以我用以下方式改变了我的方法,使一切都像魅力一样:

public Object Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture)
{
    try
    {
        String text = parameter.ToString();
        IExpression expression = null;

        if (!m_Expressions.TryGetValue(text, out expression))
            m_Expressions[text] = expression = m_Parser.Parse(text);

        Decimal result = expression.Eval(values);

        if (targetType.IsGenericType && (targetType.GetGenericTypeDefinition() == typeof(Nullable<>)))
            targetType = Nullable.GetUnderlyingType(targetType);

        if (targetType == typeof(Decimal))
            return result;

        if (targetType == typeof(Double))
            return (Double)result;

        if (targetType == typeof(Int32))
            return (Int32)result;

        if (targetType == typeof(Int64))
            return (Int64)result;

        if (targetType == typeof(String))
            return result.ToString();
    }
    catch { }

    return DependencyProperty.UnsetValue;
}