WPF two-way binding not working

后端 未结 4 1001
迷失自我
迷失自我 2020-12-16 15:30

I have a data context (UserPreferences) assigned to my main window, and a textbox that binds two-way to a property within one of the data context\'s properties

4条回答
  •  醉梦人生
    2020-12-16 15:47

    Not an answer but wanted to post the code that works on my machine to help OP...

    Complete xaml page...

    
        
            
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
                    
                    
                
                
            
        
    
    

    Complete code behind...

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows;
    using System.Xml.Serialization;
    
    namespace WpfApplication1
    {
        /// 
        /// Interaction logic for Window1.xaml
        /// 
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                DataContext = new UserPreferences();
            }
    
            private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
            {
                ((UserPreferences) DataContext).SelectedCollectionDevice.ComPort = 11;
            }
    
        }
    
        /// 
        /// Provides a series of user preferences.
        /// 
        [Serializable]
        public class UserPreferences : INotifyPropertyChanged
        {
            private CollectionDevice selectedCollectionDevice;
    
            public UserPreferences()
            {
                this.AvailableCollectionDevices = new List();
    
                var yuma1 = new CollectionDevice
                {
                    BaudRate = 4800,
                    ComPort = 31,
                    DataPoints = 1,
                    Name = "Trimble Yuma 1",
                    WAAS = true
                };
    
                var yuma2 = new CollectionDevice
                {
                    BaudRate = 4800,
                    ComPort = 3,
                    DataPoints = 1,
                    Name = "Trimble Yuma 2",
                    WAAS = true
                };
    
                var toughbook = new CollectionDevice
                {
                    BaudRate = 4800,
                    ComPort = 3,
                    DataPoints = 1,
                    Name = "Panasonic Toughbook",
                    WAAS = true
                };
    
    
                var other = new CollectionDevice
                {
                    BaudRate = 0,
                    ComPort = 0,
                    DataPoints = 0,
                    Name = "Other",
                    WAAS = false
                };
    
                this.AvailableCollectionDevices.Add(yuma1);
                this.AvailableCollectionDevices.Add(yuma2);
                this.AvailableCollectionDevices.Add(toughbook);
                this.AvailableCollectionDevices.Add(other);
    
                this.SelectedCollectionDevice = this.AvailableCollectionDevices.First();
            }
    
            /// 
            /// Gets or sets the GPS collection device.
            /// 
            public CollectionDevice SelectedCollectionDevice
            {
                get
                {
                    return selectedCollectionDevice;
                }
                set
                {
                    selectedCollectionDevice = value;
                    this.OnPropertyChanged("SelectedCollectionDevice");
                }
            }
    
            /// 
            /// Gets or sets a collection of devices that can be used for collecting GPS data.
            /// 
            [XmlIgnore]
            public List AvailableCollectionDevices { get; set; }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            /// 
            /// Notifies objects registered to receive this event that a property value has changed.
            /// 
            /// The name of the property that was changed.
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
        }
    
        /// 
        /// CollectionDevice model
        /// 
        [Serializable]
        public class CollectionDevice : INotifyPropertyChanged
        {
            /// 
            /// Gets or sets the COM port.
            /// 
            private int comPort;
    
            /// 
            /// Gets or sets a value indicating whether [waas].
            /// 
            private bool waas;
    
            /// 
            /// Gets or sets the data points.
            /// 
            private int dataPoints;
    
            /// 
            /// Gets or sets the baud rate.
            /// 
            private int baudRate;
    
            /// 
            /// Gets or sets the name.
            /// 
            public string Name { get; set; }
    
            /// 
            /// Gets or sets the COM port.
            /// 
            public int ComPort
            {
                get
                {
                    return this.comPort;
                }
    
                set
                {
                    this.comPort = value;
                    this.OnPropertyChanged("ComPort");
                }
            }
    
            /// 
            /// Gets or sets the COM port.
            /// 
            public bool WAAS
            {
                get
                {
                    return this.waas;
                }
    
                set
                {
                    this.waas = value;
                    this.OnPropertyChanged("WAAS");
                }
            }
    
            /// 
            /// Gets or sets the COM port.
            /// 
            public int DataPoints
            {
                get
                {
                    return this.dataPoints;
                }
    
                set
                {
                    this.dataPoints = value;
                    this.OnPropertyChanged("DataPoints");
                }
            }
    
            /// 
            /// Gets or sets the COM port.
            /// 
            public int BaudRate
            {
                get
                {
                    return this.baudRate;
                }
    
                set
                {
                    this.baudRate = value;
                    this.OnPropertyChanged("BaudRate");
                }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            /// 
            /// Notifies objects registered to receive this event that a property value has changed.
            /// 
            /// The name of the property that was changed.
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
                }
            }
    
            public override string ToString()
            {
                return this.Name;
            }
        }
    }
    

提交回复
热议问题