【发布时间】:2010-03-31 10:44:29
【问题描述】:
我在将 List 绑定到 DataGrid 元素时遇到问题。我创建了一个实现 INotifyPropertyChange 并保留订单列表的类:
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));
}
}
在 xaml 中有一个绑定 OrderList 元素的简单 DataGrid 组件:
<data:DataGrid x:Name="OrderList" ItemsSource="{**Binding OrderList**, Mode=TwoWay}" Height="500" Width="250" Margin="0,0,0,0" VerticalAlignment="Center"
我在 GUI 中也有一个按钮,用于向 OrderList 添加一个元素:
order.OrderList.Add("item");
DataContext 设置为全局对象:
Order order = new Order();
OrderList.DataContext = order;
问题是当我单击按钮时,该项目不会出现在 dataGrid 中。单击网格行后出现。它看起来像 INotifyPropertyChange 不起作用...... 我做错了什么??
请帮忙:)
【问题讨论】:
标签: silverlight collections binding