Answered by:
Refresh ObservableCollection

Question
-
Hi,
Well i create one observableCollection with data of my dataclasses, but when changed data in my dataclasses (Linq To SQL), the observableCollection don't refresh and i need do this to force the refresh:
this.UsersObservable = null; this.UsersObservable = new ObservableCollection<Users>(DataClasses.Users);
How i solve the problem in the best way?
How i Bind the observableCollection with my table in the DataClasses?
Friday, April 9, 2010 6:41 PM
Answers
-
You need to notify the UI that the ObservableCollection has changed. When you 'new' the variable, the UI is no longer looking at the original collection, so you have to manually tell it where to look again, and you do that with the INotifyPropertyChanged interface like so..
XAML
<ListView ItemsSource="{Binding Users}"></ListView>
CODE BEHIND
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.ComponentModel;
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window, INotifyPropertyChanged
{
DispatcherTimer _refreshTimer = new DispatcherTimer();
ObservableCollection<string> _users = new ObservableCollection<string>();
List<string> _dummyDataClass = new List<string>();
int _dummyDataIndex;
public Window1()
{
InitializeComponent();
_refreshTimer.Interval = TimeSpan.FromSeconds(2);
_refreshTimer.Tick += new EventHandler(_refreshTimer_Tick);
_refreshTimer.Start();
this.DataContext = this;
}
public ObservableCollection<string> Users
{
get { return _users; }
set
{
_users = value;
OnPropertyChanged("Users");
}
}
void _refreshTimer_Tick(object sender, EventArgs e)
{
_dummyDataClass.Add(string.Format("Item {0}", _dummyDataIndex++));
Users = new ObservableCollection<string>(_dummyDataClass);
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
}
Warm regards,Matt- Marked as answer by Thiago MDTech Tuesday, April 13, 2010 3:01 PM
Tuesday, April 13, 2010 12:17 AM
All replies
-
Thiago,
ObservableCollection notifies the UI of any changes to it. As far as I know when you set the collection to null, you destroy the collection and may also lose the binding. I think your best choice is to clear the data and add the new records.
Hope this helps.
noorbakhsh حميد نوربخشFriday, April 9, 2010 8:00 PM -
But the problem is when two users silmultaneous use my program, the DataClasses when changed by one user refresh in the other user automatic, but the observableCollection don't refresh with the DataClasses.Friday, April 9, 2010 8:52 PM
-
The code
this.UsersObservable = new ObservableCollection<Users>(DataClasses.Users);
initializes the ObservableCollection by enumerating DataClasses.Users and copying the items. It takes a snapshot. The OC doesn't listen for change notifications from DataClasses. It can't - it never saw DataClasses, but only an IEnumerable obtained from DataClasses.Users. And it's moot anyway, since Linq doesn't even raise change notifications.To put it another way, changes you make to DataClasses don't magically appear in DataClasses.Users, and even if they did they don't appear in the snapshot that you used to create the ObservableCollection.
Friday, April 9, 2010 9:33 PM -
Hi Sam,
I don't undestand your answer.
My problem not have solution?
Saturday, April 10, 2010 12:23 AM -
ObservableCollection only raise event changes for changes in the collection (Add, remove, move and clear element).
It does not raise changes when you update properties of a contained class.
To do what you want you will need implement INotifyPropertyChanged in the User class and raise the PropertyChanged event in each setter.
http://weblogs.asp.net/marianor/Saturday, April 10, 2010 3:39 AM -
Hi Mariano,
I Think that you don't undestand my problem, i know all this about the ObservableCollection, but my problem is the link of my observableCollection with my Table in DataClasses(Linq To Sql).
Don't have how bind and i need one solution.
- Proposed as answer by mroizo40 Wednesday, October 8, 2014 2:12 AM
Saturday, April 10, 2010 1:19 PM -
You can try update in this way:
var view = CollectionViewSource.GetDefaultView(this.UsersObservable); view.Refresh();
http://weblogs.asp.net/marianor/Saturday, April 10, 2010 4:21 PM -
Hi Mariano,
I Tried this but don't work.
Sunday, April 11, 2010 7:00 PM -
Hi Mariano,
I Tried this but don't work.
Sunday, April 11, 2010 7:01 PM -
Are you using that observable collection as Source of a Binding ?
http://weblogs.asp.net/marianor/Monday, April 12, 2010 12:01 AM -
Yes.Monday, April 12, 2010 12:27 AM
-
I think I misanderstood your problem. I am assuming that each time that you need to update your users collection you are creating a new collection, if that is the case the just implement INotifyPropertyChanged in the class containing UsersObservable property and raise the propertyChanged event for that collection:
this.UsersObservable = new ObservableCollection<Users>(DataClasses.Users); // Raise PropertyChanged event in "UsersObservable"
http://weblogs.asp.net/marianor/Monday, April 12, 2010 1:19 AM -
Mariano,
I yet did this but don't work.
public ObservableCollection<Users> UsersObservable { get { return _UsersObservable; } set { if (_UsersObservable != value) { _UsersObservable = value; OnPropertyChanged("UsersObservable"); } } }
Monday, April 12, 2010 2:05 AM -
I'm probably not understanding the problem correctly either, but if you're expecting your database to update the ObservableCollection it well, just won't as far as I know. You could try using a DispatchTimer that calls OnPropertyChanged("UsersObservable"); and see if that does the trick....Monday, April 12, 2010 3:27 AM
-
Hi MSearles
I think this good solution for me, but if i do DispatchTimer,
I'll have to do this?
this.UsersObservable = null;
this.UsersObservable = new ObservableCollection<Users>(DataClasses.Users);Monday, April 12, 2010 4:09 PM -
You need to notify the UI that the ObservableCollection has changed. When you 'new' the variable, the UI is no longer looking at the original collection, so you have to manually tell it where to look again, and you do that with the INotifyPropertyChanged interface like so..
XAML
<ListView ItemsSource="{Binding Users}"></ListView>
CODE BEHIND
using System.Collections.ObjectModel;
using System.Windows.Threading;
using System.ComponentModel;
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window, INotifyPropertyChanged
{
DispatcherTimer _refreshTimer = new DispatcherTimer();
ObservableCollection<string> _users = new ObservableCollection<string>();
List<string> _dummyDataClass = new List<string>();
int _dummyDataIndex;
public Window1()
{
InitializeComponent();
_refreshTimer.Interval = TimeSpan.FromSeconds(2);
_refreshTimer.Tick += new EventHandler(_refreshTimer_Tick);
_refreshTimer.Start();
this.DataContext = this;
}
public ObservableCollection<string> Users
{
get { return _users; }
set
{
_users = value;
OnPropertyChanged("Users");
}
}
void _refreshTimer_Tick(object sender, EventArgs e)
{
_dummyDataClass.Add(string.Format("Item {0}", _dummyDataIndex++));
Users = new ObservableCollection<string>(_dummyDataClass);
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
}
#endregion
}
Warm regards,Matt- Marked as answer by Thiago MDTech Tuesday, April 13, 2010 3:01 PM
Tuesday, April 13, 2010 12:17 AM -
Hi MSearles,
Good Idea, you solved my problem.
Tuesday, April 13, 2010 3:03 PM