简体   繁体   English

更新ObservableCollection之后,没有使用Datagrid Edit的新行

[英]No new row with Datagrid Editing after updating ObservableCollection

I have been having an issue with a DataGrid not making a new empty row after a new record has been entered. 我遇到了一个问题,即在输入新记录后, DataGrid无法创建新的空行。

It seems to only occur after updating the ObversableCollection<T> . 它似乎仅在更新ObversableCollection<T>之后发生。

I use this to bind to the collection: 我用它来绑定到集合:

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>();
}

This is my code for updating my ObversableCollection<T> : 这是我的代码,用于更新我的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);
            }
        }
    }
}

And this is the XAML: 这就是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>

And I use this method to update the dataGrid : 我使用这种方法来更新dataGrid

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

So everything works fine, the DataGrid' is updated, the database is correctly updated and the ObversableCollection is updated. However the 这样,一切正常, DataGrid' is updated, the database is correctly updated and theDataGrid' is updated, the database is correctly updated and the ObversableCollection is updated. However the is updated. However the DataGrid` should be added a new row after the new record is inserted, but this is what it does. is updated. However the插入新记录后,应该将DataGrid添加到新行,但这就是它的作用。

This is what it does, 就是这样

在此处输入图片说明

However, commenting out this line CollectionLists.CalculationTableSourceCollection(CalculationTblSourceObserv, @"section", @"sectionAll"); 但是,注释掉这一行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: CalculationTableGrid_OnCellEditEnding事件中,它可以正确运行,并在创建新记录后添加一个空行,请参见下文:

在此处输入图片说明

What is going on here, I can't get this to work correctly, any help would be much appreciated. 这是怎么回事,我无法正常工作,我们将不胜感激。

It seems like you are updating property CalculationTblSourceObserv but you have bounded the property ListItems in the xaml. 似乎您正在更新属性CalculationTblSourceObserv但已在xaml中限制了属性ListItems so either bind CalculationTblSourceObserv in xaml or you can do following. 因此,可以在xaml中绑定CalculationTblSourceObserv或执行以下操作。

In your question I can't see which part of code does the property 'ListItems' is present, which is the property binded to the datagrid. 在您的问题中,我看不到属性“ ListItems”存在于代码的哪一部分,即绑定到数据网格的属性。 Hopefully this is in a view model. 希望这是在视图模型中。

So either your property 'ListItems' is not getting updated or if it is getting updated then your xaml is not notified about that change. 因此,要么您的属性“ ListItems”没有更新,要么如果它正在更新,则不会通知您的xaml该更改。 Your view model code should notify the xaml that a property has been updated. 您的视图模型代码应通知xaml属性已更新。 This is done via Raising a property changed event which can be done like this: 这是通过引发属性更改事件来完成的,可以这样进行:

public string ListItems
{
    get { return listItems; }
    set
    {
         listItems= value;
         // Call OnPropertyChanged whenever the property is updated
         OnPropertyChanged("ListItems");
     }
}

  // Create the OnPropertyChanged method to raise the event
  protected void OnPropertyChanged(string name)
  {
      PropertyChangedEventHandler handler = PropertyChanged;
      if (handler != null)
      {
          handler(this, new PropertyChangedEventArgs(name));
      }
  }

Further refer to link: How to: Implement Property Change Notification 进一步参考链接: 如何:实现属性更改通知

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当添加新项事件将升起,然后立即清除CalculationTableSourceCollectionCalculationTableSourceCollection方法。

You don't have to call the CalculationTableSourceCollection method in the CellEditEnding event handler at all. 您根本不必在CellEditEnding事件处理程序中调用CalculationTableSourceCollection方法。

Any item that you add or remove to an ObservableCollection<T> will automatically show up in the DataGrid . 您添加到ObservableCollection<T>任何项目将自动显示在DataGrid This is what makes an ObservableCollection<T> different from a List<T> . 这就是使ObservableCollection<T>List<T> The latter doesn't implement the INotifyCollectionChanged interface. 后者不实现INotifyCollectionChanged接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM