WPF Datagrid 行编辑“ENDED”事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3938040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:14:12  来源:igfitidea点击:

WPF Datagrid Row Editing "ENDED" event

wpfwpfdatagridobservablecollection

提问by Alex

I know that WPF datagrid has "RowEditEnding" event , but I need to fire the event on after the Row has comitted to check if the newly added row is duplicated and merge the duplicated row. My datagrid has "CanUserAddRow" property set to True.

我知道 WPF 数据网格有“RowEditEnding”事件,但我需要在 Row 提交后触发该事件,以检查新添加的行是否重复并合并重复的行。我的数据网格将“CanUserAddRow”属性设置为 True。

I am using EntityObservableCollection that extends ObservableCollection to synchronize my entity with the collection. So, i considered OnCollectionChanged event, but the "InsertItem" event is raise once user click on the new item place holder row, which means the object is still empty and I cant check for duplicate.

我正在使用 EntityObservableCollection 扩展 ObservableCollection 来同步我的实体与集合。因此,我考虑了 OnCollectionChanged 事件,但是一旦用户单击新项目占位符行,就会引发“InsertItem”事件,这意味着该对象仍然是空的,我无法检查重复项。

Is there anyway that I can raise the RowEditEnded event?

无论如何,我可以引发 RowEditEnded 事件吗?

Thanks...

谢谢...

回答by Maher Ben Issa

    private void dgrid_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        if (this.dgrid.SelectedItem != null)
        {
            (sender as DataGrid).RowEditEnding -=dgrid_RowEditEnding;
            (sender as DataGrid).CommitEdit();
            (sender as DataGrid).Items.Refresh();
            (sender as DataGrid).RowEditEnding += dgrid_RowEditEnding;
        }
        else Return;

       //then check if the newly added row is duplicated
    }

回答by M Usman Shahid

I found an answer to your question usingVS2010

我使用VS2010找到了你问题的答案

condition if (e.EditAction == DataGridEditAction.Commit) in the RowEditEnding will fulfill ur requirement

RowEditEnding 中的条件 if (e.EditAction == DataGridEditAction.Commit) 将满足您的要求

Please see the below code.

请看下面的代码。

private void dataGrid1_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    if (e.EditAction == DataGridEditAction.Commit)
    {
        MessageBox.Show("asd");
    }
}

This is the Xaml Behind.

这是后面的Xaml。

<DataGrid AutoGenerateColumns="False" CanUserAddRows="True" Height="241" 
    RowEditEnding="dataGrid1_RowEditEnding" HorizontalAlignment="Left" 
    Name="dataGrid1" VerticalAlignment="Top" Width="573" >
    <DataGrid.Columns>
        <DataGridTextColumn Header="name" Binding="{Binding id}" 
            Width="300">
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

回答by Ozzy

Taking from @MaherBenIssa's answer, I used this to avoid add and remove delegate:

从@MaherBenIssa 的回答中,我用它来避免添加和删除委托:

    private bool locker = true;

    private void dgArticles_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
    {
        if (locker)
        {
            try{
                locker = false;
                (sender as DataGrid).CommitEdit(DataGridEditingUnit.Row, false);
                ((sender as FrameworkElement).DataContext as ViewModel)?.Edit(e.Row.DataContext);
            }
            finally{
               locker = true; //enable editing again
            }
        }
    }

回答by Shloime Rosenblum

You can use UpdateSourceTrigger=PropertyChangedon the binding of the property member for the datagrid. This will ensure that when CellEditEndingis fired the update has already been reflected in the observable collection. see this post https://stackoverflow.com/a/27239243/9285072

您可以UpdateSourceTrigger=PropertyChanged在数据网格的属性成员的绑定上使用。这将确保在CellEditEnding触发时更新已经反映在可观察集合中。看到这篇文章https://stackoverflow.com/a/27239243/9285072

回答by byMuchi

Maybe you can use the event "CurrentCellChanged". This fires when you start editing a cell too, but maybe you can find a way to not always do everything you want to do in your method.

也许您可以使用事件“CurrentCellChanged”。这也会在您开始编辑单元格时触发,但也许您可以找到一种方法来不总是在您的方法中完成您想做的所有事情。

I had same problem when i wanted to check if the changes in the datagrid are actually changes to the original values. This event is working for my needs.

当我想检查数据网格中的更改是否实际上是对原始值的更改时,我遇到了同样的问题。此活动正在满足我的需求。

Hope i could tell you something new.

希望我能告诉你一些新的东西。

回答by DrMarbuse

VB.NET solution to the solution of @MaherBenIssa

VB.NET解决@MaherBenIssa的解决方案

Private Sub dgLayer_RowEditEnding(sender As Object, e As DataGridRowEditEndingEventArgs)

    Dim d As DataGrid
    d = DirectCast(sender, DataGrid)

    RemoveHandler d.RowEditEnding, AddressOf dgLayer_RowEditEnding

    dgLayer.CommitEdit()
    sender.Items.Refresh()

    AddHandler d.RowEditEnding, AddressOf dgLayer_RowEditEnding

End Sub

回答by allthingscs

Try setting the CommitEdit() function for your datagrid. I used it here:

尝试为您的数据网格设置 CommitEdit() 函数。我在这里使用它:

private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    this.MyDataGrid.CommitEdit(DataGridEditingUnit.Row, false);
}

回答by mohammad jannesary

I wonder why you are finding the way to raise the RowEditEnded event; if you Implement the RowEditEnding eventof datagrid; whenever you edited a row and change the focus from that row, the row will be committed and RowEditEnding will be raised;

我想知道您为什么要找到引发 RowEditEnded 事件的方法;如果你实现了datagrid的 RowEditEnding 事件;每当您编辑一行并从该行更改焦点时,将提交该行并引发 RowEditEnding;

so after the Row has committed RowEditEnding will be raised and work just as RowEditEnded;

所以在 Row 提交后 RowEditEnding 将被提升并像 RowEditEnded 一样工作;

Did I understand something wrong from your text?

我从你的文字中理解错了吗?