【问题标题】:Silverlight 3 - How to "refresh" a DataGrid content?Silverlight 3 - 如何“刷新” DataGrid 内容?
【发布时间】:2010-12-22 01:07:12
【问题描述】:

我有以下风景:

1    using System;
2    using System.Windows;
3    using System.Windows.Controls;
4    using System.Windows.Documents;
5    using System.Windows.Ink;
6    using System.Windows.Input;
7    using System.Windows.Media;
8    using System.Windows.Media.Animation;
9    using System.Windows.Shapes;
10   using System.Collections.Generic;
11   
12   namespace refresh
13   {
14      public partial class MainPage : UserControl
15      {
16          
17          List c = new List();
18   
19          public MainPage()
20          {
21              // Required to initialize variables
22              InitializeComponent();
23              c.Add(new Customer{ _nome = "Josimari", _idade = "29"});
24              c.Add(new Customer{_nome = "Wesley", _idade = "26"});
25              c.Add(new Customer{_nome = "Renato",_idade = "31"});    
26              
27              this.dtGrid.ItemsSource = c;
28          }
29   
30          private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
31          {
32              c.Add(new Customer{_nome = "Maiara",_idade = "18"});
33          }
34          
35      }
36      
37      public class Customer
38      {
39          public string _nome{get; set;}
40          public string _idade{get; set;}
41      }
42   }

哪里,dtGrid 是我的 DataGrid 控件...

问题是:如何在我的列表中再添加一个寄存器后更新 UI。

我要解决它,将 DataGrid 的项目源设置为“”,然后再次设置为客户对象列表,如下所示:

1    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
2    
3    {
4    
5    c.Add(new Customer{_nome = "Maiara",_idade = "18"});
6    
7    this.dtGrid.ItemsSource="";
8    
9    this.dtGrid.ItemsSource=c;
10   
11   }
12   

有没有办法在更新、更改或删除列表 c 中的项目后自动刷新 UI 或数据网格的 itemsSource?

谢谢,

乔西马里·马塔雷利

【问题讨论】:

    标签: c# silverlight datagrid


    【解决方案1】:

    我建议研究绑定、INotifyPropertyChanged、DataContexts 和 ObservableCollection。

    1. 将您的 List 更改为 ObservableCollection。仅此一项可能就可以解决您的一些问题...因为ObservableCollection 比简单的List 更强大一点

    2. 但是,如果您想解决更多类似的问题,您将考虑通过 Xaml <DataGrid... ItemsSource="{Binding MyList}"> 将您的 DataGrid 绑定到您的对象列表

    3. 为了将您的DataGrid 绑定到您的List,您需要设置视图的DataContext。这可以很简单,就像在您的构造函数中放置:this.DataContext = this; 设置您的 DataContext 告诉视图在看到 Binding 语句时在哪里查找数据

    4. 然后,要解决您遇到的问题(更改某些内容不会刷新视图),您将在您的类上实现INotifyPropertyChanged 接口。这将允许后面的代码向视图发送通知,以便在发生变化时告诉它。

    以下是实现此功能的 C# 代码:

    public partial class MainPage : UserControl, INotifyPropertyChanged
    {
        private ObservableCollection<Customer> _MyList = 
            new ObservableCollection<Customer>();
        public ObservableCollection<Customer> MyList
        {
            get { return _MyList; }  
        } 
    
        public MainPage()
        {                      
            InitializeComponent();
    
            this.DataContext = this;
    
            MyList.Add(new Customer{ _nome = "Josimari", _idade = "29"});
            MyList.Add(new Customer{_nome = "Wesley", _idade = "26"});
            MyList.Add(new Customer{_nome = "Renato",_idade = "31"});
    
            OnPropertyChanged("MyList"); // This only works if you use bindings.
        }
    
        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            MyList.Add(new Customer{_nome = "Maiara",_idade = "18"});   
    
            OnPropertyChanged("MyList"); // This only works if you use bindings.
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged( string propertyName )
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        } 
    }
    

    【讨论】:

    • Jeremiah,即使使用 3º 版本的 Silverlight,是否仍然需要使用 ObservableCollection 来通知属性更改?谢谢你,Josimari Martarelli
    • 根据 MSDN 站点 (msdn.microsoft.com/en-us/library/ms668604(VS.95).aspx),可观察集合应在添加或删除项目时通知...但在项目更新时不会通知。正因为如此,我总是推荐列表更改时通知的模式。这是一个好习惯。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2016-10-30
    • 2011-09-21
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多