2011-04-18 81 views
3

我有一個包含double X一個視圖模型類和double Y變量,我想這個綁定到BezierSegment,似乎並不在這裏工作是我的代碼...WPF綁定到點在BezierSegment

public class TestViewModel:ViewModelBase 
{ 
    public TestViewModel() 
    { 
     TStart = new TPoint {X=20.0,Y=45.0 }; 
     TEnd = new TPoint { X = 200.0, Y = 450.0 }; 

    } 
    public TPoint TStart { get; set; } 
    public TPoint TEnd { get; set; } 

} 


public class TPoint:ViewModelBase 
{ 

    private double _X; 
    public double X 
    { 
     get { return _X; } 
     set 
     { 
      if (_X != value) 
      { 
       _X = value; 
       RaisePropertyChanged("X"); 
      } 
     } 
    } 

    private double _Y; 
    public double Y 
    { 
     get { return _Y; } 
     set 
     { 
      if (_Y != value) 
      { 
       _Y = value; 
       RaisePropertyChanged("Y"); 
      } 
     } 
    } 


} 

}

和XAML

<Window.DataContext> 
    <vm:TestViewModel /> 
</Window.DataContext> 
<Grid> 
    <Path Stroke="Black" StrokeThickness="3"> 
     <Path.Data> 
      <PathGeometry> 
       <PathGeometry.Figures> 
        <PathFigureCollection> 
         <PathFigure> 
          <PathFigure.Segments> 
           <PathSegmentCollection> 
            <BezierSegment> 
             <BezierSegment.Point1> 
              <Point X="{Binding TStart.X}" Y="{Binding TStart.Y}" /> 
             </BezierSegment.Point1> 
             <BezierSegment.Point3> 
              <Point X="{Binding TEnd.X}" Y="{Binding TEnd.Y}" /> 
             </BezierSegment.Point3> 
            </BezierSegment> 
           </PathSegmentCollection> 
          </PathFigure.Segments> 
         </PathFigure> 
        </PathFigureCollection> 
       </PathGeometry.Figures> 
      </PathGeometry> 
     </Path.Data> 
    </Path> 
</Grid> 

我得到一個錯誤,只能爲DependencyObject的DependencyProperties定義X和Y的綁定... ...

我不想依賴Windows Class Point ... 雖然這不會依賴於這個例子。

有人可以告訴我如何將我自己的點座標綁定到BezierSegemnt Point1 Point2 Point3?

回答

1

你將不得不做這樣的:

public class TPoint:ViewModelBase 
{ 

    private double _X; 
    public double X 
    { 
     get { return _X; } 
     set 
     { 
      if (_X != value) 
      { 
       _X = value; 
       RaisePropertyChanged("X"); 
       RaisePropertyChanged("P"); 
      } 
     } 
    } 

    private double _Y; 
    public double Y 
    { 
     get { return _Y; } 
     set 
     { 
      if (_Y != value) 
      { 
       _Y = value; 
       RaisePropertyChanged("Y"); 
       RaisePropertyChanged("P"); 
      } 
     } 
    } 

    public Point P { get { return new Point(X,Y);}}  
} 

,並在XAML:

<Window.DataContext> 
    <vm:TestViewModel /> 
</Window.DataContext> 
<Grid> 
    <Path Stroke="Black" StrokeThickness="3"> 
     <Path.Data> 
      <PathGeometry> 
       <PathGeometry.Figures> 
        <PathFigureCollection> 
         <PathFigure> 
          <PathFigure.Segments> 
           <PathSegmentCollection> 
            <BezierSegment Point1="{Binding TStart.P}" Point3="{Binding TEnd.P}"/> 
           </PathSegmentCollection> 
          </PathFigure.Segments> 
         </PathFigure> 
        </PathFigureCollection> 
       </PathGeometry.Figures> 
      </PathGeometry> 
     </Path.Data> 
    </Path> 
</Grid> 

讓我知道,如果它的工作,我遠不及開發環境

+0

我有這樣的事情在我的腦海,我想知道是否有可能結合X和Y不含天然Point類......我'll試試看吧.. – silverfighter 2011-04-18 17:31:46

+0

@silverfighter BezierSegment只接受'Point'結構。因爲這不是'DependencyObject',X和Y不是DependencyProperties,所以你不能像這樣使用它,但必須爲DependencyProperties'Point1-3'設置一個完整的'Point'。我所展示的替代方案將是一個帶MultiValueConverter的MultiBinding,它返回一個「Point」。 – 2011-04-18 17:37:17

0
  1. 包裝點 - >類BindingPoint實現INotifyPropertyChanged接口

    public class BindingPoint : INotifyPropertyChanged 
    { 
        private Point point; 
    
        public BindingPoint(double x, double y) 
        { 
         point = new Point(x, y); 
        } 
    
        public double X 
        { 
         get { return point.X; } 
         set 
         { 
          point.X = value; 
          OnPropertyChanged(); 
          OnPropertyChanged("Point"); 
         } 
        } 
    
        public double Y 
        { 
         get { return point.Y; } 
         set 
         { 
          point.Y = value; 
          OnPropertyChanged(); 
          OnPropertyChanged("Point"); 
         } 
        } 
    
        public Point Point 
        { 
         get { return point; } 
        } 
    
        public event PropertyChangedEventHandler PropertyChanged; 
    
        [NotifyPropertyChangedInvocator] 
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
        { 
         var handler = PropertyChanged; 
         if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 
        } 
    } 
    
  2. 綁定到BezierSegment:

    private Path DefineBezierSegment(BindingPoint startPoint, BindingPoint endPoint, BindingPoint startBezierPoint, BindingPoint endBezierPoint) 
    { 
        BezierSegment spline = new BezierSegment {IsStroked = true}; 
    
        var b = new Binding("Point") 
        { 
         Source = startBezierPoint, 
         Mode = BindingMode.TwoWay 
        }; 
        BindingOperations.SetBinding(spline, BezierSegment.Point1Property, b); 
    
        b = new Binding("Point") 
        { 
         Source = endBezierPoint, 
         Mode = BindingMode.TwoWay 
        }; 
        BindingOperations.SetBinding(spline, BezierSegment.Point2Property, b); 
    
        b = new Binding("Point") 
        { 
         Source = endPoint, 
         Mode = BindingMode.TwoWay 
        }; 
        BindingOperations.SetBinding(spline, BezierSegment.Point3Property, b); 
    
        var pColl = new PathSegmentCollection {spline}; 
    
        var pFig = new PathFigure(StartPort.Origin.Point, pColl, false); 
    
        b = new Binding("Point") 
        { 
         Source = startPoint, 
         Mode = BindingMode.TwoWay 
        }; 
        BindingOperations.SetBinding(pFig, PathFigure.StartPointProperty, b); 
    
        var pfColl = new PathFigureCollection { pFig }; 
    
        return new Path() {Data = new PathGeometry(pfColl)}; 
    }