WPF绑定到BezierSegment中的点

3

我有一个ViewModel类,其中包含double Xdouble Y变量,我想将其绑定到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类Point...尽管即使对于这个例子也行不通。

有人能告诉我如何将自己的点坐标绑定到BezierSegemnt的Point1、Point2、Point3上吗?

2个回答

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>

请告诉我它是否有效,我离开发环境很远


我脑海中有类似的想法,不知道是否可能在没有本地Point类的情况下绑定X和Y... 不过我会尝试一下的。 - silverfighter
@silverfighter,BezierSegment仅接受“Point”结构。由于它不是“DependencyObject”,而且X和Y也不是DependencyProperties,因此您不能像那样使用它,但必须为DependencyProperties“Point1-3”设置整个“Point”。除我所展示的之外,另一种选择是使用MultiBinding和返回“Point”的MultiValueConverter。 - Markus Hütter

0
点的包装器——使用INotifyPropertyChanged接口实现的BindingPoint类
``` 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)); } } ```
将BezierSegment绑定到BindingPoint:
``` 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)}; } ```

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接