Wpf 数据网格在滚动时挂起/冻结
我对WPF中的datagrid有一个奇怪的问题。我正在为我的应用程序使用MVVM模式,我的视图模型实现idataerrorinfo接口。每当我在添加新行后在数据网格中上下滚动时,所有单元格都会混乱起来,整个数据网格都会冻结。如果我删除idataerrorinfo接口实现,它可以正常工作。有人有同样的问题吗 .任何帮助都将不胜感激 更新: 只有在向dataGrid添加新行后,才会出现这种奇怪的行为。如果修改现有行并上下滚动不会导致任何问题。将新viewmodel添加到我的可观察集合时发生了一些事情。不知道是什么。我需要一些帮助 更新: 这是这个项目的一个小版本 XAMLWpf 数据网格在滚动时挂起/冻结,wpf,mvvm,datagrid,Wpf,Mvvm,Datagrid,我对WPF中的datagrid有一个奇怪的问题。我正在为我的应用程序使用MVVM模式,我的视图模型实现idataerrorinfo接口。每当我在添加新行后在数据网格中上下滚动时,所有单元格都会混乱起来,整个数据网格都会冻结。如果我删除idataerrorinfo接口实现,它可以正常工作。有人有同样的问题吗 .任何帮助都将不胜感激 更新: 只有在向dataGrid添加新行后,才会出现这种奇怪的行为。如果修改现有行并上下滚动不会导致任何问题。将新viewmodel添加到我的可观察集合时发生了一些事情
不要在IDataErrorInfo这个[string columnName]获取程序中执行耗时的IO操作。制造
System.IO.File.AppendAllText("C:\\temp\\log.txt", "PartConfigName: " + PartConfigName + "\r\n");
或者
[条件(“调试”)]
我已删除该IO操作部分。但是仍然有相同的错误。事实上,我只是把它放在那个里,以检查控制进行验证的次数…现在我有了一些新的发现。。请检查更新部分。在AddNewConfigPropertyExecute()
中,您有两个AllConfig.Add()
,可能这就是问题所在。PS:为了快速检查,我使用了Console.Write()
。对于额外的AllConfig.Add(),非常抱歉。我已经把它处理掉了。但并没有解决我的问题。我创建了一个小型项目,不包括所有实体框架的东西。但是仍然有同样的问题。只有当我添加一行并尝试编辑和滚动时才会发生这种情况。收藏品出了问题。不知道是什么。如果您愿意,我可以将代码发送给您代码>是不需要的。ObservableCollection负责处理新项目。
namespace testWPF
{
class PersonListViewModel: ViewModelBase
{
private ObservableCollection<Person> personCollection;
//private PartNumbersEntities dbCOntext = new PartNumbersEntities();
public ObservableCollection<Person> GetPeople
{
get
{
if (personCollection == null)
{
personCollection = new ObservableCollection<Person>();
for(int i= 0; i<100;i++)
{
personCollection.Add(new Person()
{
Name = "Frank Grimmes",
Age = 25,
DateOfBirth = new DateTime(1975, 2, 19)
});
}
}
return personCollection;
}
}
public ICommand AddNewConfigProperty { get { return new RelayCommand(AddNewConfigPropertyExecute, CanAddNewConfigPropertyExecute); } }
bool CanAddNewConfigPropertyExecute()
{
return true;
}
void AddNewConfigPropertyExecute()
{
personCollection.Add(new Person()
{
Name = "Some Name",
Age = 25,
DateOfBirth = new DateTime(1924, 9, 1)
});
OnPropertyChanged("GetPeople");
}
}
}
namespace testWPF
{
public class Person : ViewModelBase, IDataErrorInfo
{
//private readonly Regex nameEx = new Regex(@"^[A-Za-z ]+$");
private string name;
public string Name
{
get { return name; }
set
{
name = value;
}
}
private int age;
public int Age
{
get { return age; }
set
{
age = value;
}
}
public DateTime DateOfBirth { get; set; }
public string Error
{
get { return ""; }
}
public string this[string columnName]
{
get
{
string result = null;
if (columnName == "Name")
{
if (string.IsNullOrEmpty(Name))
result = "Please enter a name";
}
return result;
}
}
}
}
System.IO.File.AppendAllText("C:\\temp\\log.txt", "PartConfigName: " + PartConfigName + "\r\n");