DataBinding to Calculated Field

前端 未结 5 1617
忘了有多久
忘了有多久 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-13 23:55

    I'm assuming Student implements INotifyPropertyChanged. what you have to do is to register to the PropertyChanged Event for LodgingCosts + RegistrationCosts + TravelCosts + DiningCosts, and raise the PropertyChanged event for TotalCosts.

    public partial class Student
    {
        public Decimal TotalCosts
        {
            get { return (LodgingCosts + RegistrationCosts + TravelCosts + DiningCosts); }
        }
    
        public Student()
        {
            this.PropertyChanged += new PropertyChangedEventHandler(Student_PropertyChanged);
        }
    
        void Student_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "LodgingCosts" ||
                e.PropertyName == "RegistrationCosts" ||
                e.PropertyName == "TravelCosts" ||
                e.PropertyName == "DiningCosts")
            {
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("TotalCosts"));
            }
        }
    
    }
    

提交回复
热议问题