wpf twoway绑定DependencyProperty setcurrentvalue不起作用

时间:2013-09-23 15:00:46

标签: wpf binding two-way-binding setcurrentvalue

我正在研究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没有相应地更新值?

1 个答案:

答案 0 :(得分:0)

发现问题。

我在ViewModel中的属性是一个小数,下面一行返回的属性是double。

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

我为绑定添加了一个转换器,它按预期工作。