Silverlight 3 - How to "refresh" a DataGrid content?

14,044

I recommend looking into binding, INotifyPropertyChanged, DataContexts and ObservableCollection.

  1. Change your List into an ObservableCollection. That alone will probably solve some of your problems... as an ObservableCollection is a little more robust than a simple List

  2. But, if you want to solve more problems like this, you will look into binding your DataGrid to your list of objects through Xaml <DataGrid... ItemsSource="{Binding MyList}">

  3. In order to bind your DataGrid to your List, you will need to set the DataContext of the View. This can be a simple as placing in your constructor: this.DataContext = this; Setting your DataContext tells the view where to look for data when it sees Binding statements

  4. Then, to solve problems like the one you have (changing something doesn't refresh the view), you will implement the INotifyPropertyChanged interface on your class. This will allow the code behind to send notifications to the view to tell it when something changes.

Here's the C# code that would implement this:

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));
        }
    } 
}
Share:
14,044
Learning Xamarin
Author by

Learning Xamarin

Updated on June 05, 2022

Comments

  • Learning Xamarin
    Learning Xamarin almost 2 years

    I have the following scenery:

    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   }
    

    Where, dtGrid is my DataGrid control...

    The Question is: How to get the UI Updated after adding one more register to my list.

    I get to solve it setting the DataGrid's Item Source to "" and then setting to the list of Customer objects again, like that:

    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   
    

    Is there a way to get the UI updated or the datagrid's itemsSource refreshed automatically after updating, altering or deleting an item from the list c ?

    Thank you,

    Josimari Martarelli

  • Learning Xamarin
    Learning Xamarin over 14 years
    Jeremiah, Even using the 3º Version of Silverlight, is it still necessary using the ObservableCollection to notify property changes? Thank You, Josimari Martarelli
  • Jeremiah
    Jeremiah over 14 years
    According to the MSDN site ( msdn.microsoft.com/en-us/library/ms668604(VS.95).aspx ) The observable collection should notify when Items are Added or Removed... But it won't notify when items are Updated. Because of this, I always recommend the pattern of Notifying whenever the list changes. It's a good habit. Good luck!