将列表绑定到 DataGrid Silverlight

发布于 2024-08-27 23:05:27 字数 1423 浏览 2 评论 0原文

我在将列表绑定到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2024-09-03 23:05:27

INotifyPropertyChange 工作正常,因为将新项目添加到现有 List 的代码实际上并未将新值重新分配给 OrderList 属性(即 >set 例程永远不会被调用),也不会调用 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)); 
        } 
    }   
}

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 the OrderList property (that is the set routine is never called) there is no call to NotifyPropertyChanged. Try it like this:-

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)); 
        } 
    }   
}

The ObservableCollection<T> type supports informing INotifyCollectionChanged which will inform the DataGrid when items are added to or removed from the collection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文