Data transfer to the calculation field

I'm having a little problem when I try to associate a DataTextColumn DataGrid with a computed 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 an Entity object with one small addition. TotalCosts is not a field in db tables, so for this I created an incomplete class.

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

The problem I encountered is that TotalCosts does not automatically update when filling out any of the other fields. I assume this is because it is not listed as a dependency property. How to resolve this for a property where there is no set?

+4
source share
4 answers

You can call OnPropertyChanged("TotalCosts") in the settings of each property that TotalCosts depends TotalCosts , it will update the binding

+3
source

I assume Student implements INotifyPropertyChanged. you need to register in the EventChanged 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")); } } } 
+3
source

Hello, I think I can help you, I have this code for a database in sqlServer and I have a data source with a DataSet for my database

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using TestDeDataGrid.TestDSTableAdapters; namespace TestDeDataGrid { public partial class MainWindow : Window { //private ICollection<TablaTest> registros; public MainWindow() { InitializeComponent(); TestDS ds = new TestDS(); TablaTestTableAdapter adapter = new TablaTestTableAdapter(); adapter.Fill(ds.TablaTest); TablaTestGrid.ItemsSource = ds.TablaTest.DefaultView; } private void TablaTestGrid_CurrentCellChanged(object sender, EventArgs e) { if ((String)(((DataGrid)sender).CurrentColumn.Header) == "A/B") ((DataGrid)sender).CommitEdit(DataGridEditingUnit.Row, true); } } } 

With this trick, column C is calculated automatically after I have a value in columns A and B, for example, if I put 10 in columna A and 5 in column B then I just press Tab to go to column C, and the value 2 appears automatically there is no need to press another line or press the enter key.

Column C is a computed column with an expression (ColumnaA / ColumnaB) and of type System.Decimal.

And there is my xaml code:

 <DataGrid Name="TablaTestGrid" Grid.Row="1" AutoGenerateColumns="False" CurrentCellChanged="TablaTestGrid_CurrentCellChanged"> <DataGrid.Columns> <DataGridTextColumn Header="ID" Binding="{Binding Path=IdTablaTest}" IsReadOnly="True"></DataGridTextColumn> <DataGridTextColumn Header="Col A" Binding="{Binding Path=ColumnaA}"></DataGridTextColumn> <DataGridTextColumn Header="Col B" Binding="{Binding Path=ColumnaB}"></DataGridTextColumn> <DataGridTextColumn Header="A/B" Binding="{Binding Path=ColumnaC}" IsReadOnly="True" ></DataGridTextColumn> <DataGridTextColumn Header="TestOnly"></DataGridTextColumn> </DataGrid.Columns> </DataGrid> 
+3
source

You need to implement INotifyPropertyChanged in the class. Here is an 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:

 <DockPanel> <TextBox Text="{Binding P1.Id}" Height="50" Width="100" DockPanel.Dock="Top" /> <TextBox Text="{Binding P1.Name}" Height="50" Width="100" DockPanel.Dock="Top" /> <Button Content="OK" Click="Button_Click" DockPanel.Dock="Bottom"/> </DockPanel> 

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) { } 

enter something into 2 text fields .. and click on the button to see the value of the person P1..u, you will find the calculated field with the value .. hope this helps you. Thanks, BHavik

0
source

Source: https://habr.com/ru/post/1345306/


All Articles