Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataBinding to Calculated Field

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

WPF

<DataGrid ItemsSource="{Binding Path=CurrentRoster, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
          AutoGenerateColumns="False" 
          AlternatingRowBackground="Gainsboro"  
          AlternationCount="2">
    <DataGrid.Columns>
    <DataGridComboBoxColumn Header="Student Enrolled"
                            ItemsSource="{Binding Source={StaticResource AvailableStudents}}"
                            SelectedItemBinding="{Binding Path=Student}">
    </DataGridComboBoxColumn>
    <DataGridTextColumn Header="Registration" Binding="{Binding Path=RegistrationCosts, StringFormat='{}{0:C}'}"/>
    <DataGridTextColumn Header="Lodging" Binding="{Binding Path=LodgingCosts, StringFormat='{}{0:C}'}"/>
    <DataGridTextColumn Header="Travel" Binding="{Binding Path=TravelCosts, StringFormat='{}{0:C}'}"/>
    <DataGridTextColumn Header="Dining" Binding="{Binding Path=DiningCosts, StringFormat='{}{0:C}'}"/>
    <DataGridTextColumn Header="Total Costs" IsReadOnly="True" Binding="{Binding Path=TotalCosts, StringFormat='{}{0:C}'}"/>
</DataGrid.Columns>

Where Student is a Entity object with one small addition. TotalCosts isn't a field on the db tables, so I created a partial class for this.

public partial class Student
{
    public  Decimal TotalCosts
    {
        get { return (LodgingCosts + RegistrationCosts + TravelCosts + DiningCosts); }
    }
}

The problem I'm experiencing is that TotalCosts is not automatically updating when you fill in any of the other fields. My guess it is because it is not listed as a dependency property. How do I resolve this for a property where there is no set ?

like image 637
Scott Avatar asked Dec 22 '22 16:12

Scott


2 Answers

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"));
        }
    }

}
like image 189
Elad Katz Avatar answered Dec 31 '22 02:12

Elad Katz


You can call OnPropertyChanged("TotalCosts") in the setters of each property that TotalCosts depends on, it will refresh the binding

like image 31
Thomas Levesque Avatar answered Dec 31 '22 00:12

Thomas Levesque