DataBinding to Calculated Field

前端 未结 5 1586
忘了有多久
忘了有多久 2021-01-13 23:46

I\'m running into a small problem where I\'m trying to bind a DataTextColumn of a DataGrid to a Calculated Field.

WPF



        
5条回答
  •  余生分开走
    2021-01-14 00:02

    You need to implement the INotifyPropertyChanged on the class. Here is the Example:

    public class Person : INotifyPropertyChanged
    {
        //public int Id
        //{ get; set; }
    
        //public string Name { get; set; }
    
        private int _Id;
    
        public int Id
        {
            get { return _Id; }
            set { _Id = value;
            RaisePropertyChanged("Id");
            }
        }
    
        private string _EmpNo
        {
            get
            {
                return Id.ToString() + Name.ToString();
            }
        }
    
        private string _Name;
    
        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
                RaisePropertyChanged("Name");
            }
        }
    
        private void RaisePropertyChanged(string property)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property));
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    }
    

    XAML code:

    
        
        
        

    Test:

    public TestWindow()
    {
        InitializeComponent();
    
        this.DataContext = this;
    }
    
    private Person _P1 = new Person();
    
    public Person P1
    {
        get { return _P1; }
        set { _P1 = value; }
    }
    
    private void Button_Click(object sender, RoutedEventArgs e)
    {
    
    }
    

    type something in the 2 textboxes..and on the Button click see the value of the person P1 ..u will find the calculated field with the value.. hope it helps u.. Thanks, BHavik

提交回复
热议问题