2009-12-01 66 views
2

我有以下的风光:Silverlight 3 - 如何“刷新”DataGrid内容?

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 

是否有一种方式来获得更新的用户界面或DataGrid的itemsSource在更新,更改或删除列表c中的项目后自动刷新?

谢谢

Josimari Martarelli

回答

8

我建议考虑结合,INotifyPropertyChanged的,DataContexts和的ObservableCollection。

  1. 更改您的ListObservableCollection。这本身就可能会解决一些你的问题......作为ObservableCollection不仅仅是一个简单的List

  2. 多了几分稳健但是,如果你想解决更多的问题是这样,你会看着你的DataGrid绑定到您的通过XAML <DataGrid... ItemsSource="{Binding MyList}">

  3. 对象列表为了绑定你DataGridList,你将需要设置视图的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)); 
     } 
    } 
} 
+0

耶利米, 即使使用3°的Silverlight版本,它仍然需要使用的ObservableCollection通知性质的变化? 谢谢, Josimari Martarelli – 2009-12-01 19:46:40

+1

根据MSDN站点(http://msdn.microsoft.com/en-us/library/ms668604(VS.95).aspx)可观察的集合应该在项目添加时通知或删除...但它不会通知何时更新项目。因此,我总是建议每当列表更改时通知模式。这是一个好习惯。祝你好运! – Jeremiah 2009-12-01 22:04:01