When I change the value of any cell in a row, I want to dynamically change other cells in the same row. For example, in the following grid, when I change 1 to 3 in the second row, the value of 5 must change to 3. (The edit button only saves changes into the database.)
Here's the xaml code of my DataGrid
. I have tried using SelectedCellsChanged
and SelectionChanged
events but I wasn't successful.
<DataGrid x:Name="MyDataGrid" x:Uid="MyDataGrid" AutoGenerateColumns="False"
Height="226" HorizontalAlignment="Left" Margin="106,111,0,0"
VerticalAlignment="Top" Width="684" ColumnWidth="*"
AlternationCount="2" SelectionMode="Single"
SelectedCellsChanged="MyDataGrid_SelectedCellsChanged"
SelectionChanged="MyDataGrid_SelectionChanged" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=ProductName}" Header="Ürün Adı" />
<DataGridTextColumn Binding="{Binding Path=StoreName}" Header="Şube Adı" />
<DataGridTextColumn Binding="{Binding Path=Day1}" Header="Pazartesi" />
<DataGridTextColumn Binding="{Binding Path=Day2}" Header="Salı" />
<DataGridTextColumn Binding="{Binding Path=Day3}" Header="Çarşamba" />
<DataGridTextColumn Binding="{Binding Path=Day4}" Header="Perşembe" />
<DataGridTextColumn Binding="{Binding Path=Day5}" Header="Cuma" />
<DataGridTextColumn Binding="{Binding Path=Day6}" Header="Cumartesi" />
<DataGridTextColumn Binding="{Binding Path=Day7}" Header="Pazar" />
<DataGridTemplateColumn Header="Edit Row">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Edit" Click="EditButton_Click" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
You have taken the wrong approach - do not try and rely on manipulating things in response to a user interface event like SelectedCellsChanged
.
Instead you can manipulate and update the related properties in the setter of the bound properties on the bound data object. If you then fire a property change notification for the affected properties (which they should be doing themselves in their own setters) then the values in the DataGrid
will be automatically updated via the binding.
Example:
this is contrived but it should show you what I mean. If your data object that each row of the grid binds to contains a bunch of properties with private backing members , and the data object implements INotifyPropertyChanged then you can do something like this:
public class MyDataClass : INotifyPropertyChanged
{
public int Day1
{
get { return _day1; }
set
{
_day1 = value;
UpdateProperties();
}
}
public int Day2
{
get { return _day2; }
set
{
_day2 = value;
UpdateProperties();
}
}
// etc, repeat for the next three 'Day' properties
private void UpdateProperties()
{
//adjust the private backing members for each property:
_day1 = someValue;
_day2 = someOtherValue;
OnPropertyChanged("Day1");
OnPropertyChanged("Day2");
//etc, notify for each property that you adjusted
}
private void OnPropertyChanged(string propertyName)
{
handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangeEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
private int _day1, _day2, //etc... ;
}
Of course when you do this you want to be very careful about setting properties - you don't want to get into a circular calling situation which will cause a stack overflow. After you have adjusted the private backing member and then fired the PropertyChanged
event for each updated property the row in the grid should be updated automatically via the binding.
handler(this, new PropertyChangeEventArgs(propertyName)); not handler(this, new PropertyChangeEventArgs(propertyName));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.