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

发布于 2024-09-27 09:08:33 字数 329 浏览 9 评论 0原文

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

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

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

谢谢...

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.

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.

Is there anyway that I can raise the RowEditEnded event?

Thanks...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

给我一枪 2024-10-04 09:08:33
    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
    }
    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
    }
你列表最软的妹 2024-10-04 09:08:33

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

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://stackoverflow.com/a/27239243/9285072

心凉怎暖 2024-10-04 09:08:33

找到了您的问题的答案

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

请参阅下面的代码。

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

这就是背后的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>

I found an answer to your question usingVS2010

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

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.

<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>
橙幽之幻 2024-10-04 09:08:33

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

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

    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
            }
        }
    }
鹤仙姿 2024-10-04 09:08:33

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

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

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

private void DataGrid_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
    this.MyDataGrid.CommitEdit(DataGridEditingUnit.Row, false);
}
瀟灑尐姊 2024-10-04 09:08:33

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

VB.NET solution to the solution of @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
对你的占有欲 2024-10-04 09:08:33

也许您可以使用事件“CurrentCellChanged”。当您开始编辑单元格时也会触发此事件,但也许您可以找到一种方法来不总是在您的方法中执行您想要执行的所有操作。

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

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

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.

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.

讽刺将军 2024-10-04 09:08:33

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

因此,在 Row 提交后,RowEditEnding 将被引发并像 RowEditEnded 一样工作;

我从您的文字中理解有误吗?

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;

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

Did I understand something wrong from your text?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文