将Point2D绑定到两个单独的textBoxes

时间:2013-10-22 12:06:46

标签: c# wpf xaml binding point

我有一个双精度的Point2D结构(带有x和y成员)。我想将Point2D的实例绑定到两个单独的TextBox。我怎么能这样做?

1 个答案:

答案 0 :(得分:3)

为什么要使用struct?写一个课

public class Point2D : INotifyPropertyChanged
{    
    public event PropertyChangedEventHandler PropertyChanged;

    private double _x;
    private double _y;    

    public double X
    {
        get { return _x; }
        set
        {
            _x = value;
            NotifyPropertyChanged("X");
        }
    }

    public double Y
    {
        get { return _y; }
        set
        {
            _y = value;
            NotifyPropertyChanged("Y");
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }    
}

DataContext中的属性

public Point2D MyPoint2D { get; set; }

XAML中的绑定

<TextBox Name="TextBoxValueX" Text="{Binding MyPoint2D.X, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Name="TextBoxValueY" Text="{Binding MyPoint2D.Y, UpdateSourceTrigger=PropertyChanged}" />

希望它有所帮助!