问题:
使用List作为DataGrid的Itemsource:
XAML:
ItemsSource="{Binding ProNames}",
ViewModel:
public List ProNames
{
get
{
return protocalModels;
}
set
{
protocalModels = value;
this.RaisePropertyChanged("ProNames");
}
}
但改变ProNames内容(新增、删除)后,Datagird显示内容不刷新。
解决办法:
1. 改变源,对ProNames设置新的数据源;
使用先ProNames=null;在赋值也是一样道理。
但不推荐这种方式。
2.使用ObservableCollection。
其说明为:
// Summary:
// Represents a dynamic data collection that provides notifications when items
// get added, removed, or when the whole list is refreshed.
//
// Type parameters:
// T:
// The type of elements in the collection.
[Serializable]
[TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
public class ObservableCollection : Collection, INotifyCollectionChanged, INotifyPropertyChanged
{
。。。
}
修改如下:
public ObservableCollection ProNames
{
get
{
return protocalModels;
}
set
使用List作为DataGrid的Itemsource:
XAML:
ItemsSource="{Binding ProNames}",
ViewModel:
public List ProNames
{
get
{
return protocalModels;
}
set
{
protocalModels = value;
this.RaisePropertyChanged("ProNames");
}
}
但改变ProNames内容(新增、删除)后,Datagird显示内容不刷新。
解决办法:
1. 改变源,对ProNames设置新的数据源;
使用先ProNames=null;在赋值也是一样道理。
但不推荐这种方式。
2.使用ObservableCollection。
其说明为:
// Summary:
// Represents a dynamic data collection that provides notifications when items
// get added, removed, or when the whole list is refreshed.
//
// Type parameters:
// T:
// The type of elements in the collection.
[Serializable]
[TypeForwardedFrom("WindowsBase, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")]
public class ObservableCollection : Collection, INotifyCollectionChanged, INotifyPropertyChanged
{
。。。
}
修改如下:
public ObservableCollection ProNames
{
get
{
return protocalModels;
}
set
当使用List作为WPF DataGrid的ItemSource并更改内容时,界面可能不会自动刷新。解决方法包括:1) 替换整个数据源,但这不推荐;2) 使用ObservableCollection,它提供项添加、移除或列表刷新时的通知,确保界面自动更新。
1946

被折叠的 条评论
为什么被折叠?



