2013-02-18 2 views
0

У меня есть следующая анимация, которая работает как шарм:WPF Ресурсы Раскадровка Связывание

<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 конвертер абсолютно правильно, я проверил это) он просто перестает работать должным образом:

<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> 

«К» значение, во время выполнения отладки, является ноль. Почему здесь не должно работать связка? Есть ли обходной путь? Ручная настройка этого значения в коде позади работает конечно ... но хорошо ...

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

ответ

0

Проблемы была как мой конвертер было значение всех возвращений обработки:

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; 
} 
Смежные вопросы