2

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.)

enter image description here

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>
Grant Winney
  • 65,241
  • 13
  • 115
  • 165
rockenpeace
  • 1,011
  • 5
  • 18
  • 29

2 Answers2

7

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.

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • how can i do this ? sorry, i am beginner in wpf. can you give any example ? – rockenpeace Jun 13 '13 at 00:36
  • This lead me in the right direction, thanks. Usually, i let my ViewModels handle all the NotifyPropertyChanged-events, but in this special case, it's ok to soften up the pattern a little bit and let the DataModels of the records in the DataGrid handle their synchronization by themself. – M463 Sep 22 '16 at 09:50
0

handler(this, new PropertyChangeEventArgs(propertyName)); not handler(this, new PropertyChangeEventArgs(propertyName));