0

我正在研究 visiblox 图表的自定义行为。此自定义行为具有一个依赖属性 Value,该属性标识由图表中的垂直线绘制组成的光标位置。如果我将属性 FollowMouse 设置为 true,则此光标会跟随鼠标。

如果我绑定 Value 属性,changedvaluecallback 只会将 0 作为 newValue,而如果未绑定该值,则它可以正常工作。但是,如果我更改绑定的源属性(ViewModel 上的属性),它也可以工作。所以问题是在 PointerMoved 上使用 SetCurrentValue 设置值。

这是行为的源代码:

    public class TimeCursorBehavior : BehaviourWithAxesBase
    {

        private System.Windows.Shapes.Line _line;

        public TimeCursorBehavior()
            : base("TimeCursor")
        {
            _line = new System.Windows.Shapes.Line();
            _line.Stroke = System.Windows.Media.Brushes.Black;
            _line.StrokeThickness = 2;
        }

        public override void DeInit()
        {
            base.DeInit();

            Chart.BehaviourCanvas.Children.Remove(_line);
        }

        protected override void Init()
        {
            base.Init();

            Chart.BehaviourCanvas.Children.Add(_line);
        }

        public override void PointerMoved(IBehaviourEventSource sender, PointerEventContext context)
        {
            base.PointerMoved(sender, context);

            if (!FollowMouse)
                return;

            IComparable xDataValue = XAxis.GetRenderPositionAsDataValueWithZoom(context.Point.X);

            SetCurrentValue(ValueProperty, xDataValue);
        }

        public override void BehaviourCanvasSizeChanged(IBehaviourEventSource sender, SizeChangedEventArgs e)
        {
            base.BehaviourCanvasSizeChanged(sender, e);

            _line.Y2 = e.NewSize.Height;
        }

        #region Value

        public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(IComparable), typeof(TimeCursorBehavior), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnValueChanged));

        private static void OnValueChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            (sender as TimeCursorBehavior).OnValueChanged(args.OldValue as IComparable, args.NewValue as IComparable);
        }

        private void OnValueChanged(IComparable oldValue, IComparable newValue)
        {
            if (XAxis == null)
                return;

            double x = XAxis.GetDataValueAsRenderPositionWithZoom(newValue);
            _line.X1 = x;
            _line.X2 = x;
        }

        public IComparable Value
        {
            get
            {
                return GetValue(ValueProperty) as IComparable;
            }
            set
            {
                SetValue(ValueProperty, value);
            }
        }

        #endregion

        #region FollowMouse

        public static readonly DependencyProperty FollowMouseProperty = DependencyProperty.Register("FollowMouse", typeof(bool), typeof(TimeCursorBehavior), new PropertyMetadata(false));

        public bool FollowMouse
        {
            get
            {
                return (bool)GetValue(FollowMouseProperty);
            }
            set
            {
                SetValue(FollowMouseProperty, value);
            }
        }

        #endregion

    }

有谁知道为什么 setcurrentvalue 没有相应地更新值?

4

1 回答 1

0

Found the problem.

My property in the ViewModel is an decimal, and the property returned by the line below is a double.

IComparable xDataValue = XAxis.GetRenderPositionAsDataValueWithZoom(context.Point.X);

I added an converter to the binding it everything worked as expected.

于 2013-09-27T12:24:53.360 回答