Silverlight 3 - How to "refresh" a DataGrid content?
I recommend looking into binding, INotifyPropertyChanged, DataContexts and ObservableCollection.
Change your
List
into anObservableCollection
. That alone will probably solve some of your problems... as anObservableCollection
is a little more robust than a simpleList
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}">
In order to bind your
DataGrid
to yourList
, you will need to set theDataContext
of the View. This can be a simple as placing in your constructor:this.DataContext = this;
Setting yourDataContext
tells the view where to look for data when it seesBinding
statementsThen, 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));
}
}
}
Learning Xamarin
Updated on June 05, 2022Comments
-
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 over 14 yearsJeremiah, Even using the 3º Version of Silverlight, is it still necessary using the ObservableCollection to notify property changes? Thank You, Josimari Martarelli
-
Jeremiah over 14 yearsAccording 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!