将列表绑定到 DataGrid Silverlight
我在将列表绑定到 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("物品”);
DataContext 设置为全局对象:
Order order = new Order();
OrderList.DataContext = order;
问题是,当我单击按钮时,该项目不会出现在 dataGrid 中。单击网格行后会出现它。看起来 INotifyPropertyChange 不起作用...... 我做错了什么?
请帮忙:)
I have a problem binding List to a DataGrid element. I've created a class that implements INotifyPropertyChange and keeps list of orders:
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));
}
}
In xaml a have a simple DataGrid component that Binds the OrderList element:
<data:DataGrid x:Name="OrderList" ItemsSource="{**Binding OrderList**, Mode=TwoWay}" Height="500" Width="250" Margin="0,0,0,0" VerticalAlignment="Center"
I also have a button in GUI taht adds a element to OrderList:
order.OrderList.Add("item");
The DataContext is set to the global object:
Order order = new Order();
OrderList.DataContext = order;
The problem is that when i Click the button, the item does not apear in dataGrid. It apears after a click on a grid row. It seams like INotifyPropertyChange does not work...
What am I doing wrong??
Please HELP:)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
INotifyPropertyChange 工作正常,因为将新项目添加到现有
List
的代码实际上并未将新值重新分配给OrderList
属性(即>set
例程永远不会被调用),也不会调用NotifyPropertyChanged
。尝试像这样: -ObservableCollection
类型支持通知INotifyCollectionChanged
,它将在向 DataGrid 添加或删除项目时通知DataGrid
收藏。INotifyPropertyChange is working fine, since your code to Add a new item to the existing
List
does not actually re-assign a new value to theOrderList
property (that is theset
routine is never called) there is no call toNotifyPropertyChanged
. Try it like this:-The
ObservableCollection<T>
type supports informingINotifyCollectionChanged
which will inform theDataGrid
when items are added to or removed from the collection.