I have a class like this:
public class Example
{
public Example()
{
}
public int Count1 { get; set; }
public int Count2 { get; set; }
public int Count3 { get { return Count1 + Count2 ; } }
}
And a DataGrid with a List<Example> as ItemSource:
<DataGrid x:Name="grdExample" AutoGenerateColumns="False" CanUserAddRows="False" SelectionMode="Single" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Count1 , Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Header="Count1" />
<DataGridTextColumn Binding="{Binding Count1 , Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Header="Count1" />
<DataGridTextColumn Binding="{Binding Count2 , Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Header="Count2" />
<DataGridTextColumn Binding="{Binding Count3}" Header="Count3" />
</DataGrid.Columns>
</DataGrid>
I want that when I edit the Count1 or Count2 column , the Count3 updates too .
With this code, just the second column (that is the same property then first coumn) has been changed.
I tried to refresh Items of the grid in SelectedCellsChanged event. But causes StackOverFlow, once the event keep changing on each refresh.
What Am i missing ? I look up for the solution, but without success.
PS: Debbuging I can see that the values are correct in grdExample.Items
Implement
INotifyPropertyChangedinterface in your model classExample. Whenever the value is changed in UI, model will get notification and In your model class you can updateCount3value as and whenCount1&Count2changes.This link can help you with
INotifyPropertyChangedinterface implementation: https://learn.microsoft.com/en-us/dotnet/framework/winforms/how-to-implement-the-inotifypropertychanged-interface