0

I'm working on a custom behavior for the visiblox chart. This custom behavior has a dependency property Value that identifies the position of a cursor that consists of vertical line draw in the chart. This cursor follows the mouse if I set the property FollowMouse to true.

If I bind the Value property the changedvaluecallback only gets 0 as the newValue, while if the value is not bound it works properly. But if i change the source property of the binding (property on ViewModel) it works too. So the problem is setting the value with SetCurrentValue on PointerMoved.

Here is the source code of the behavior:

    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

    }

Does anyone know why setcurrentvalue is not updating the value accordingly?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
maykonvs
  • 31
  • 2
  • Did you define a binding to the Value dependency property and are you trying to set the ValueProperty with whatever is the new value (explicitly) is? – 123 456 789 0 Sep 23 '13 at 17:26
  • Yes, I am. If I change the value of the property in ViewModel everything works fine. The problem is setting the ValueProperty inside the behavior whith the SetCurrentValue method. – maykonvs Sep 25 '13 at 18:11

1 Answers1

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.

maykonvs
  • 31
  • 2