1

我有一个由 ObservableCollection 支持的 DataGrid。每当我更改一些数据时,我都会将整个 ObservableCollection 写入远程服务器。然后几毫秒后,我从服务器收到更新。由于设计的性质以及为了维护 DataGrid 中的选定元素,我使用来自服务器的更新一个一个地覆盖 ObservableCollection 中的元素。

问题是我可以编辑一行的单元格,然后单击另一行,我将无法编辑该单元格。在进行编辑之前,我必须再次单击另一个单元格。

我知道这与延迟后覆盖元素有关,下面的示例重现了该问题。关于发生了什么的任何想法?

xml:

<Window x:Class="WpfApplication3.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit"
Title="Window1" Height="300" Width="300">
<Grid>
  <dg:DataGrid Name="dg" AutoGenerateColumns="True"/>
</Grid>
</Window>

后面的代码:

using System;
using System.Windows;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Threading;

namespace WpfApplication3
{
   /// <summary>
   /// Interaction logic for Window1.xaml
   /// </summary>
   public partial class Window1 : Window
   {
      public Window1()
      {
         InitializeComponent();
         mPeople = new People();
         dg.ItemsSource = mPeople.mList;      
      }

      public People mPeople;
   }

   public class People : INotifyPropertyChanged
   {
      public event PropertyChangedEventHandler PropertyChanged;

      public People()
      {
         mList = new ObservableCollection<Person>();
         mList.Add(CreatePerson("Jacob", "Lewis"));
         mList.Add(CreatePerson("Scott", "Doe"));
         mList.Add(CreatePerson("Corey", "Dorn"));

         dp = new DispatcherTimer();
         dp.Tick += new EventHandler(dp_Tick);
         dp.Interval = new System.TimeSpan(0, 0, 0, 0,100);
      }

      public Person CreatePerson(string first, string last)
      {
         Person p = new Person(first, last);
         p.EditCompleted += new Person.EditCompletedDelegate(OnPersonEditComplete);
         return p;
      }

      public void OnPersonEditComplete(Person p)
      {
         dp.Start();
      }

      void dp_Tick(object sender, EventArgs e)
      {
         dp.Stop();
         for (int i = 0; i < mList.Count; i++) {
            mList[i] = CreatePerson(mList[i].FirstName, mList[i].LastName);
         }
      }

      public ObservableCollection<Person> mList;
      private DispatcherTimer dp;
   }

   public class Person : INotifyPropertyChanged, IEditableObject
   {
      public event PropertyChangedEventHandler PropertyChanged = delegate { };
      public delegate void EditCompletedDelegate(Person p);
      public EditCompletedDelegate EditCompleted;


      void IEditableObject.EndEdit()
      {
         EditCompleted(this);
      }

      void IEditableObject.BeginEdit() { }
      void IEditableObject.CancelEdit() { }

      public Person(string first, string last)
      {
         FirstName = first;
         LastName = last;
      }

      public string FirstName 
      {
         get { return mFirstName; }
         set { mFirstName = value; PropertyChanged(this, new PropertyChangedEventArgs("FirstName")); }
      }

      public string LastName
      {
         get { return mLastName; }
         set { mLastName = value; PropertyChanged(this, new PropertyChangedEventArgs("LastName")); }
      }

      private string mFirstName;
      private string mLastName;
   }
}
4

0 回答 0