您好,我有一个与 绑定的 WPF 数据网格TrulyObservableCollection,每当我尝试通过文本输入编辑单元格时,数据网格单元格在输入每个键后都会失去焦点,
public sealed class TrulyObservableCollection<T> : ObservableCollection<T>
where T : INotifyPropertyChanged
{
public TrulyObservableCollection()
{
CollectionChanged += FullObservableCollectionCollectionChanged;
}
public TrulyObservableCollection(IEnumerable<T> pItems)
: this()
{
foreach (var item in pItems) {
this.Add(item);
}
}
private void FullObservableCollectionCollectionChanged(object sender,
NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null) {
foreach (Object item in e.NewItems) {
((INotifyPropertyChanged)item).PropertyChanged += ItemPropertyChanged;
}
}
if (e.OldItems != null) {
foreach (Object item in e.OldItems) {
((INotifyPropertyChanged)item).PropertyChanged -= ItemPropertyChanged;
}
}
}
private …Run Code Online (Sandbox Code Playgroud) 我有两个具有相同值的集合,但它们具有不同的参考。在没有 foreach 语句的情况下比较两个集合的最佳方法是什么,下面是我创建的示例应用程序,
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace CollectionComparer
{
public class Program
{
private static void Main(string[] args)
{
var persons = GetPersons();
var p1 = new ObservableCollection<Person>(persons);
IList<Person> p2 = p1.ToList().ConvertAll(x =>
new Person
{
Id = x.Id,
Age = x.Age,
Name = x.Name,
Country = x.Country
});
//p1[0].Name = "Name6";
//p1[1].Age = 36;
if (Equals(p1, p2))
Console.WriteLine("Collection and its values are Equal");
else
Console.WriteLine("Collection and its values are not Equal");
Console.ReadLine();
}
public …Run Code Online (Sandbox Code Playgroud)