2

TargetUpdated イベントが完璧に思えるという問題に遭遇しました。残念ながら、Multibinding には TargetUpdated プロパティがないようです。これをまだ設定する方法はありますか、それとも完全に別の解決策を見つける必要がありますか?

役立つコードをいくつか紹介します

バインディングはこちら...

<RotateTransform x:Name="LeftNeedle">
<RotateTransform.Angle>
    <MultiBinding Converter="{StaticResource VoltageRatioConverter}" NotifyOnTargetUpdated="True" >
        <Binding ElementName="root" Path="Running" />
        <Binding ElementName="root" Path="Incoming" />
    </MultiBinding>
</RotateTransform.Angle>

次に、値コンバーターがあります...

public class VoltageRatioConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (values == null || values.Count((x) => x != null) != 2)
            return 0.0;

        double running = System.Convert.ToDouble(values[0]);
        double incoming = System.Convert.ToDouble(values[1]);

        return ((running / incoming) - 1);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

そして、私が探しているのは、その結果の数値を取得し、それを関数に渡して角度/アニメーションを更新することです

    private static void OnAngleValueChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        GaugeCluster gauge = o as GaugeCluster;
        if ((double)e.NewValue > 70)
        {
            VisualStateManager.GoToState(gauge, "RightWobble", false);
        }
        else if ((double)e.NewValue < -70)
        {
            VisualStateManager.GoToState(gauge, "LeftWobble", false);
        }
        else
        {
            if ((double)e.OldValue > 70 || (double)e.OldValue < -70)
            {
                VisualStateManager.GoToState(gauge, "StandingState", false);
            }
        }
    }

^ この関数は、2 つの依存関係プロパティのいずれかからのコールバックにすることで、実際に機能することを確認するためにテストしていました。理想的には、TargetUpdated イベントで使用したいのは関数本体です。

4

1 に答える 1

2

TargetUpdated付属イベントです。バインディング自体ではなく、バインディング ターゲットでこのイベントを登録します。

<RotateTransform x:Name="LeftNeedle" Binding.TargetUpdated="LeftNeedle_TargetUpdated">
    <RotateTransform.Angle>
        <MultiBinding Converter="{StaticResource VoltageRatioConverter}" NotifyOnTargetUpdated="True" >
            <Binding ElementName="root" Path="Running" />
            <Binding ElementName="root" Path="Incoming" />
        </MultiBinding>
    </RotateTransform.Angle>
</RotateTransform>

更新:実際には、上記の解決策は機能しないため、それが適用されるイベントを処理する必要がありますRotateTransform:UIElementBinding.TargetUpdatedUIElement

<Rectangle Fill="Blue" Width="30" Height="30"
           Binding.TargetUpdated="LeftNeedle_TargetUpdated">
  <Rectangle.RenderTransform>
    <RotateTransform x:Name="LeftNeedle">
        <RotateTransform.Angle>
            <MultiBinding Converter="{StaticResource VoltageRatioConverter}" NotifyOnTargetUpdated="True" >
                <Binding ElementName="root" Path="Running" />
                <Binding ElementName="root" Path="Incoming" />
            </MultiBinding>
        </RotateTransform.Angle>
    </RotateTransform>
  </Rectangle.RenderTransform>
</Rectangle />

イベントは にバブルアップし、イベント引数Rectangleのプロパティをチェックして、それが回転かどうかを確認できますTargetObject

于 2010-07-24T01:03:59.690 に答える