0

我为 UserControl 创建了一个 DependencyProperty,它应该在 -2 .. 2 的范围内

在属性窗口中旋转鼠标滚轮时。属性值变化 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

FrameworkPropertyMetadata选项添加到 a 的定义中时DependencyProperty,可以选择提供CoerceValueCallback处理程序。您可以更改此处理程序中的传入值。有关完整的详细信息,请参阅 MSDN 上的依赖属性回调和验证页面。从链接页面:

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