更新ObservableCollection后,没有使用Datagrid Edit的新行 [英] No new row with Datagrid Editing after updating ObservableCollection

查看:54
本文介绍了更新ObservableCollection后,没有使用Datagrid Edit的新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在输入新记录后, DataGrid 没有在新的空行中出现问题.

I have been having an issue with a DataGrid not making a new empty row after a new record has been entered.

它似乎仅在更新 ObversableCollection< T> 之后发生.

It seems to only occur after updating the ObversableCollection<T>.

我用它来绑定到集合:

public partial class MainWindow : INotifyPropertyChanged
{
    public MainWindow()
    {
    DataContext = this; 
    InitializeComponent();       

    CollectionLists.CalculationTableSourceCollection(CalculationTblSourceObserv, @"section", @"sectionAll");
    CalculationTableGrid.ItemsSource = CalculationTblSourceObserv;
    }
public ObservableCollection<CalculationListTbl> CalculationTblSourceObserv { get; set; } 
 = new ObservableCollection<CalculationListTbl>();
}

这是我的代码,用于更新我的 ObversableCollection< T> :

This is my code for updating my ObversableCollection<T>:

class CollectionLists
{
public static void CalculationTableSourceCollection(ObservableCollection<CalculationListTbl> observable,
  string section, string sectionAll)
    {
            using (DatabaseDataContext dataContext = new DatabaseDataContext(MainWindow.InstanceConnectionString))
        {
            observable.Clear();
            var source = DatabaseQueries.CalculationTableSourceAll(sectionAll, dataContext);
            if (source == null) return;
            foreach (var item in source)
            {
                observable.Add(item);
            }
        }
    }
}

这是XAML:

<DataGrid x:Name="CalculationTableGrid" Grid.Column="2" 
       AutoGenerateColumns="False" ItemsSource="{Binding}" 
       Grid.Row="1" Grid.RowSpan="12" AlternationCount="2" 
       CanUserAddRows="True" CanUserSortColumns="False" 
       CanUserDeleteRows="True" GridLinesVisibility="None" 
       CellEditEnding="CalculationTableGrid_OnCellEditEnding" 
       VerticalAlignment="Top">

    <DataGrid.Columns>
        <DataGridTextColumn Width="*" Header="項目" 
           Binding="{Binding UpdateSourceTrigger=PropertyChanged, Path=ListItems, Mode=TwoWay}" />
    </DataGrid.Columns>

</DataGrid>

然后我使用这种方法来更新 dataGrid :

And I use this method to update the dataGrid:

private void CalculationTableGrid_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
     ShiftTypeData.UserInputData.AddNewDataShiftInputRecords(e, MainUserId, EmployeesNameNumberPairsAll, CalculationTblListObserv, DateFilter);
     CollectionLists.CalculationTableSourceCollection(CalculationTblSourceObserv, @"section", @"sectionAll");               
}

因此一切正常,更新了 DataGrid',正确地更新了数据库,并且更新了 ObversableCollection .但是, DataGrid`应该在插入新记录后添加新行,但这就是它的作用.

So everything works fine, the DataGrid' is updated, the database is correctly updated and theObversableCollectionis updated. However theDataGrid` should be added a new row after the new record is inserted, but this is what it does.

这就是它的作用

但是,注释掉这一行 CollectionLists.CalculationTableSourceCollection(CalculationTblSourceObserv,@"section",@"sectionAll"); 事件,可以使它正确运行并创建新记录后,将添加一个空行,如下所示:

However, commenting out this line CollectionLists.CalculationTableSourceCollection(CalculationTblSourceObserv, @"section", @"sectionAll"); in the CalculationTableGrid_OnCellEditEnding event allows it to correctly behave and an empty row is added after the new record is made, see below:

这是怎么回事,我无法使它正常工作,任何帮助将不胜感激.

What is going on here, I can't get this to work correctly, any help would be much appreciated.

推荐答案

此处的问题是,在添加新项目时会引发 CellEditEnding 事件,然后立即清除您的 CalculationTableSourceCollection 方法中的CalculationTableSourceCollection .

The issue here is that the CellEditEnding event will be raised when the new item is added and then you immediately clear your CalculationTableSourceCollection in your CalculationTableSourceCollection method.

您完全不必在 CellEditEnding 事件处理程序中调用 CalculationTableSourceCollection 方法.

You don't have to call the CalculationTableSourceCollection method in the CellEditEnding event handler at all.

您添加或删除到 ObservableCollection< T> 的任何项目都会自动显示在 DataGrid 中.这就是使 ObservableCollection< T> List< T> 不同的原因.后者未实现

Any item that you add or remove to an ObservableCollection<T> will automatically show up in the DataGrid. This is what makes an ObservableCollection<T> different from a List<T>. The latter doesn't implement the INotifyCollectionChanged interface.

这篇关于更新ObservableCollection后,没有使用Datagrid Edit的新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆