WPF数据网格行编辑“结束”事件

时间:2022-09-12 08:43:40

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 datagrid有“roweditend”事件,但是我需要在行到达之后启动该事件,以检查新添加的行是否重复,并合并重复的行。我的datagrid将“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”事件会在用户单击新项place holder行后引发,这意味着对象仍然为空,我无法检查是否重复。

Is there anyway that I can raise the RowEditEnded event?

有什么办法可以让我提起roweditend事件吗?

Thanks...

谢谢……

7 个解决方案

#1


22  

    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
    }

#2


2  

I found an answer to your question usingVS2010

我用vs2010找到了你问题的答案

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

如果条件(e。在rowedi中将满足ur的要求。

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>

#3


0  

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

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

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

#4


0  

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

我想知道你为什么要想办法提出这个问题;如果您实现了datagrid的rowedi事件;每当您编辑一个行并从该行中更改焦点时,该行将被提交并将引发rowedi;

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

因此,在争吵之后,rowediting将被提升,并像roweditend一样工作;

Did I understand something wrong from your text?

我从你的课文中理解错了什么吗?

#5


0  

VB.NET solution to the solution of @MaherBenIssa

VB。@MaherBenIssa的解决方案的NET solution

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

#6


0  

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
            }
        }
    }

#7


0  

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

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

#1


22  

    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
    }

#2


2  

I found an answer to your question usingVS2010

我用vs2010找到了你问题的答案

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

如果条件(e。在rowedi中将满足ur的要求。

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>

#3


0  

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

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

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

#4


0  

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

我想知道你为什么要想办法提出这个问题;如果您实现了datagrid的rowedi事件;每当您编辑一个行并从该行中更改焦点时,该行将被提交并将引发rowedi;

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

因此,在争吵之后,rowediting将被提升,并像roweditend一样工作;

Did I understand something wrong from your text?

我从你的课文中理解错了什么吗?

#5


0  

VB.NET solution to the solution of @MaherBenIssa

VB。@MaherBenIssa的解决方案的NET solution

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

#6


0  

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
            }
        }
    }

#7


0  

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

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