0

-2 .. 2 の範囲にある必要がある UserControl の DependencyProperty を作成しました

プロパティ ウィンドウでマウスのスクロール ホイールを回転させたとき。プロパティ値が 1 つ変化します。そして、値を 0.1 ずつ変更したい DependencyProperty でステップ変更を設定するにはどうすればよいですか? XAML エディターでプロパティを操作します。

 public double Value
        {
            get { return (double)GetValue(BarValueProperty); }
            set { SetValue(BarValueProperty, value); }
        }


        public static readonly DependencyProperty BarValueProperty =
        DependencyProperty.Register("Value", typeof(double), typeof(MeterBar), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.AffectsRender));
4

1 に答える 1

0

When adding the FrameworkPropertyMetadata options into the definition of a DependencyProperty, there is an option to provide a CoerceValueCallback handler. You can change the incomming values in this handler. For complete details, please see the Dependency Property Callbacks and Validation page on MSDN. From the linked page:

public static readonly DependencyProperty CurrentReadingProperty = 
    DependencyProperty.Register(
    "CurrentReading",
    typeof(double),
    typeof(Gauge),
    new FrameworkPropertyMetadata(
        Double.NaN,
        FrameworkPropertyMetadataOptions.AffectsMeasure,
        new PropertyChangedCallback(OnCurrentReadingChanged),
        new CoerceValueCallback(CoerceCurrentReading)
    ),
    new ValidateValueCallback(IsValidReading)
);
public double CurrentReading
{
  get { return (double)GetValue(CurrentReadingProperty); }
  set { SetValue(CurrentReadingProperty, value); }
}

...

private static object CoerceCurrentReading(DependencyObject d, object value)
{
    // Do whatever calculation to update your value you need to here
    Gauge g = (Gauge)d;
    double current = (double)value;
    if (current
            <g.MinReading) current = g.MinReading;
    if (current >g.MaxReading) current = g.MaxReading;
    return current;
}
于 2013-10-21T10:29:40.883 に答える