C# 了解用户是否更改了DataGrid中的数据的最佳方法是什么?

C# 了解用户是否更改了DataGrid中的数据的最佳方法是什么?,c#,wpf,datagrid,C#,Wpf,Datagrid,我想知道用户每次修改WPF DataGrid中的数据 是否有一个单一的事件,我可以用来做这件事?或者,我可以用来覆盖全套数据更改(添加行、删除行、修改行等)的最小事件集是什么?通常,当您使用MVVM时,您将主列表绑定到一个ObservableCollection,然后将所选项绑定到一个特定实例。在二传中,你可以引发事件。这将是捕获数据列表的更新/添加/删除的最合乎逻辑的方法(读:我见过的最常用的方法) 通常,在使用MVVM时,会将主列表绑定到ObservableCollection,然后将所选项

我想知道用户每次修改WPF DataGrid中的数据


是否有一个单一的事件,我可以用来做这件事?或者,我可以用来覆盖全套数据更改(添加行、删除行、修改行等)的最小事件集是什么?

通常,当您使用MVVM时,您将主列表绑定到一个ObservableCollection,然后将所选项绑定到一个特定实例。在二传中,你可以引发事件。这将是捕获数据列表的更新/添加/删除的最合乎逻辑的方法(读:我见过的最常用的方法)

通常,在使用MVVM时,会将主列表绑定到ObservableCollection,然后将所选项绑定到特定实例。在二传中,你可以引发事件。这将是捕获数据列表的更新/添加/删除的最合乎逻辑的方法(读:我见过的最常用的方法)

main window.xaml

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication"
        Title="MainWindow" Height="350" Width="525">
    <DataGrid x:Name="dataGrid" AutoGeneratingColumn="OnAutoGeneratingColumn">
        <DataGrid.Resources>
            <Style TargetType="DataGridCell">
                <EventSetter Event="Binding.SourceUpdated" Handler="OnDataGridCellSourceUpdated"/>
                <EventSetter Event="Binding.TargetUpdated" Handler="OnDataGridCellTargetUpdated"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.dataGrid.ItemsSource = new ObservableCollection<Person>()
        {
            new Person() { Name = "John", Surname = "Doe" },
            new Person() { Name = "Jane", Surname = "Doe" }
        };
    }

    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        var dataGridBoundColumn = e.Column as DataGridBoundColumn;
        if (dataGridBoundColumn != null)
        {
            var binding = dataGridBoundColumn.Binding as Binding;
            if (binding != null)
            {
                binding.NotifyOnSourceUpdated = true;
                binding.NotifyOnTargetUpdated = true;
            }
        }
    }

    private void OnDataGridCellSourceUpdated(object sender, DataTransferEventArgs e)
    {
        this.OnDataGridCellChanged((DataGridCell)sender);
    }

    private void OnDataGridCellTargetUpdated(object sender, DataTransferEventArgs e)
    {
        this.OnDataGridCellChanged((DataGridCell)sender);
    }

    private void OnDataGridCellChanged(DataGridCell dataGridCell)
    {
        // DataContext is MS.Internal.NamedObject for NewItemPlaceholder row.
        var person = dataGridCell.DataContext as Person;
        if (person != null)
        {
            var propertyName = ((Binding)((DataGridBoundColumn)dataGridCell.Column).Binding).Path.Path;
            var propertyValue = TypeDescriptor.GetProperties(person)[propertyName].GetValue(person);
            // TODO: do some logic here.
        }
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.dataGrid.ItemsSource=新的ObservableCollection()
{
新人(){Name=“John”,姓氏=“Doe”},
新人(){Name=“Jane”,姓氏=“Doe”}
};
}
AutoGeneratingColumn上的私有void(对象发送方,DataGridAutoGeneratingColumnEventArgs e)
{
var dataGridBoundColumn=e.列作为dataGridBoundColumn;
如果(dataGridBoundColumn!=null)
{
var binding=dataGridBoundColumn.binding为binding;
if(绑定!=null)
{
binding.NotifyOnSourceUpdated=true;
binding.NotifyOnTargetUpdated=true;
}
}
}
私有void OnDataGridCellSourceUpdated(对象发送方、数据传输目标)
{
此.OnDataGridCellChanged((DataGridCell)发送方);
}
私有void OnDataGridCellTargetUpdated(对象发送方、数据传输目标)
{
此.OnDataGridCellChanged((DataGridCell)发送方);
}
私有void OnDataGridCellChanged(DataGridCell DataGridCell)
{
//DataContext是NewItemPlaceholder行的MS.Internal.NamedObject。
var person=dataGridCell.DataContext作为person;
if(person!=null)
{
var propertyName=((Binding)((DataGridBoundColumn)dataGridCell.Column).Binding).Path.Path;
var propertyValue=TypeDescriptor.GetProperties(person)[propertyName].GetValue(person);
//TODO:在这里做一些逻辑分析。
}
}
}
这就是我在基于Person(只是一些POCO)实例、属性名和属性值的复杂DataGridCell格式设置中使用的内容


但是,如果您想知道何时保存数据并使用MVVM,那么最好的方法是为视图模型/模型中的每个可编辑属性设置原始值和当前值。加载数据时,原始值和当前值将相等,若通过DataGrid或任何其他方式更改属性,则只更新当前值。当需要保存数据时,只需检查任何项是否有任何具有不同原始值和当前值的属性。如果回答是“是”,则应保存数据,因为自上次加载/保存后数据已更改,否则数据与加载时相同,因此不需要重新保存。此外,在保存时,必须将当前值复制为原始值,因为数据再次等于保存的数据,就像上次加载时一样。

MainWindow.xaml

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication"
        Title="MainWindow" Height="350" Width="525">
    <DataGrid x:Name="dataGrid" AutoGeneratingColumn="OnAutoGeneratingColumn">
        <DataGrid.Resources>
            <Style TargetType="DataGridCell">
                <EventSetter Event="Binding.SourceUpdated" Handler="OnDataGridCellSourceUpdated"/>
                <EventSetter Event="Binding.TargetUpdated" Handler="OnDataGridCellTargetUpdated"/>
            </Style>
        </DataGrid.Resources>
    </DataGrid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.dataGrid.ItemsSource = new ObservableCollection<Person>()
        {
            new Person() { Name = "John", Surname = "Doe" },
            new Person() { Name = "Jane", Surname = "Doe" }
        };
    }

    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        var dataGridBoundColumn = e.Column as DataGridBoundColumn;
        if (dataGridBoundColumn != null)
        {
            var binding = dataGridBoundColumn.Binding as Binding;
            if (binding != null)
            {
                binding.NotifyOnSourceUpdated = true;
                binding.NotifyOnTargetUpdated = true;
            }
        }
    }

    private void OnDataGridCellSourceUpdated(object sender, DataTransferEventArgs e)
    {
        this.OnDataGridCellChanged((DataGridCell)sender);
    }

    private void OnDataGridCellTargetUpdated(object sender, DataTransferEventArgs e)
    {
        this.OnDataGridCellChanged((DataGridCell)sender);
    }

    private void OnDataGridCellChanged(DataGridCell dataGridCell)
    {
        // DataContext is MS.Internal.NamedObject for NewItemPlaceholder row.
        var person = dataGridCell.DataContext as Person;
        if (person != null)
        {
            var propertyName = ((Binding)((DataGridBoundColumn)dataGridCell.Column).Binding).Path.Path;
            var propertyValue = TypeDescriptor.GetProperties(person)[propertyName].GetValue(person);
            // TODO: do some logic here.
        }
    }
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
this.dataGrid.ItemsSource=新的ObservableCollection()
{
新人(){Name=“John”,姓氏=“Doe”},
新人(){Name=“Jane”,姓氏=“Doe”}
};
}
AutoGeneratingColumn上的私有void(对象发送方,DataGridAutoGeneratingColumnEventArgs e)
{
var dataGridBoundColumn=e.列作为dataGridBoundColumn;
如果(dataGridBoundColumn!=null)
{
var binding=dataGridBoundColumn.binding为binding;
if(绑定!=null)
{
binding.NotifyOnSourceUpdated=true;
binding.NotifyOnTargetUpdated=true;
}
}
}
私有void OnDataGridCellSourceUpdated(对象发送方、数据传输目标)
{
此.OnDataGridCellChanged((DataGridCell)发送方);
}
私有void OnDataGridCellTargetUpdated(对象发送方、数据传输目标)
{
此.OnDataGridCellChanged((DataGridCell)发送方);
}
私有void OnDataGridCellChanged(DataGridCell DataGridCell)
{
//DataContext是NewItemPlaceholder行的MS.Internal.NamedObject。
var person=dataGridCell.DataContext作为person;
if(person!=null)
{
var propertyName=((Binding)((DataGridBoundColumn)dataGridCell.Column).Binding).Path.Path;
var propertyValue=TypeDescriptor.GetProperties(person)[propertyName].GetValue(person);
//TODO:在这里做一些逻辑分析。
}
}
}
这就是我在基于Person(只是一些POCO)实例、属性名和属性值的复杂DataGridCell格式设置中使用的内容

但是,如果您想知道何时保存数据并使用MVVM,那么最好的方法是为视图模型/模型中的每个可编辑属性设置原始值和当前值。加载数据时,原始值和当前值将相等,若通过DataGrid或