2013-09-23 35 views
0

我正在處理visiblox圖表的自定義行爲。此自定義行爲具有依賴項屬性值,該值標識由圖表中垂直線條繪製組成的光標的位置。如果將屬性FollowMouse設置爲true,則此光標跟隨鼠標。wpf twoway bound DependencyProperty setcurrentvalue不起作用

如果我綁定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沒有相應地更新的價值?

+0

您是否定義了一個綁定到Value依賴項屬性,並且您是否嘗試使用任何新值(明確)來設置ValueProperty? –

+0

是的,我是。如果我更改ViewModel中的屬性的值,一切正常。問題是將ValueProperty設置爲SetCurrentValue方法的行爲。 – maykonvs

回答

0

發現問題。

我在ViewModel中的屬性是一個小數,而下面一行返回的屬性是double。

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

我添加了一個轉換器綁定它一切按預期工作。