【问题标题】:Binding a Point2D to two separate textBoxes将 Point2D 绑定到两个单独的文本框
【发布时间】:2013-10-22 12:06:46
【问题描述】:

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

【问题讨论】:

    标签: c# wpf xaml binding point


    【解决方案1】:

    为什么要使用结构?写一堂课

    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}" />
    

    希望对你有帮助!

    【讨论】:

    • +1 此外,由于您实现了 INotifyPropertyChanged,您还应该添加 Text="{Binding MyPoint2D.X, UpdateSourceTrigger=PropertyChanged}" 以及 Y。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    相关资源
    最近更新 更多