将列表绑定到DataGrid Silverlight
将列表绑定到DataGrid元素时出现问题。我创建了一个实现INotifyPropertyChange并保留订单列表的类:将列表绑定到DataGrid Silverlight,silverlight,collections,binding,Silverlight,Collections,Binding,将列表绑定到DataGrid元素时出现问题。我创建了一个实现INotifyPropertyChange并保留订单列表的类: public class Order : INotifyPropertyChanged { private String customerName; public String CustomerName { get { return customerName; } set { cu
public class Order : INotifyPropertyChanged
{
private String customerName;
public String CustomerName
{
get { return customerName; }
set {
customerName = value;
NotifyPropertyChanged("CustomerName");
}
}
private List<String> orderList = new List<string>();
public List<String> OrderList
{
get { return orderList; }
set {
orderList = value;
NotifyPropertyChanged("OrderList");
}
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
问题是,当我单击该按钮时,该项不在dataGrid中显示。它在单击网格行后显示。就像INotifyPropertyChange不起作用一样。。。
我做错了什么
请提供帮助:)INotifyPropertyChange工作正常,因为向现有的
列表添加新项的代码实际上不会将新值重新分配给OrderList
属性(即从未调用集
例程),因此不会调用NotifyPropertyChanged
。试着这样做:-
public class Order : INotifyPropertyChanged
{
private String customerName;
public String CustomerName
{
get { return customerName; }
set {
customerName = value;
NotifyPropertyChanged("CustomerName");
}
}
private ObservableCollection<String> orderList = new ObservableCollection<String>();
public ObservableCollection<String> OrderList
{
get { return orderList; }
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
公共类顺序:INotifyPropertyChanged
{
私有字符串客户名称;
公共字符串客户名称
{
获取{return customerName;}
集合{
客户名称=价值;
NotifyPropertyChanged(“客户名称”);
}
}
私有ObservableCollection orderList=新ObservableCollection();
公共可观察收集订单列表
{
获取{返回订单列表;}
}
public void NotifyPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
observedcollection
类型支持通知INotifyCollectionChanged
,当项目添加到集合或从集合中移除时,该类型将通知DataGrid
Order order = new Order();
OrderList.DataContext = order;
public class Order : INotifyPropertyChanged
{
private String customerName;
public String CustomerName
{
get { return customerName; }
set {
customerName = value;
NotifyPropertyChanged("CustomerName");
}
}
private ObservableCollection<String> orderList = new ObservableCollection<String>();
public ObservableCollection<String> OrderList
{
get { return orderList; }
}
public void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}