green1111
green1111

Reputation: 231

ObservableCollection Refresh is not working, when dynamic allocated

public ObservableCollection<Model.ChildCommand> ChildCommands { get; private set; }

ChildCommands is bound to a Datagrid.

Both Segments show me the items in the Datagrid.

But in Segment one the Datagrid items doesn't refresh automatically when the collection changes. With Segment two, the refresh works.

Segment 1:

var childs = from child in m_context.ChildCommand.Local
                         select child;


this.ChildCommands = new ObservableCollection<Model.ChildCommand>(childs);

Segment 2:

this.ChildCommands = m_context.ChildCommand.Local;

How do I get the automatic refresh by using Segment one?

Upvotes: 4

Views: 113

Answers (4)

user469104
user469104

Reputation: 1236

The reason for Segment 1 not updating automatically is that you end up binding to a different ObservableCollection instance.

When m_context.ChildCommand.Local changes, your Segment 2 datagrid gets notified because it is bound to that observable collection instance. However your Segment 1 datagrid is bound to a different observable collection instance (that you yourself create when you say new ObservableCollection(childs).

If you truly want both of them to be bound to the m_context.ChildCommand.Local observable collection then you should implement it as such instead of creating a different observable collection instance for Segment 1.

Upvotes: 2

Mike Eason
Mike Eason

Reputation: 9723

Bindings to a collection will not be updated if the collection gets re-instantiated. You have two options:

  1. Implement INotifyPropertyChanged on the ChildCommands property
  2. Instead of doing:

    this.ChildCommands = new ObservableCollection(childs);

Do this instead:

this.ChildCommands.Clear();

foreach(var child in childs)
   this.ChildCommands.Add(child);

Upvotes: 0

Adrian Nasui
Adrian Nasui

Reputation: 1095

my solution to this is to inherit my class from the NotificationObject class.

After that, just use

this.ChildCommands = m_context.ChildCommand.Local;

RaisePropertyChanged(() => this.ChildCommands); //this line notifies the UI that the underlying property has changed.

Upvotes: 0

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26318

You need to implement INotifyPropertyChanged for the ChildCommands property, as such:

public class YourClass: INotifyPropertyChanged
  {
      private ObservableCollection<Model.ChildCommand> _childCommands;

      public event PropertyChangedEventHandler PropertyChanged;

      public ObservableCollection<Model.ChildCommand> ChildCommands
      {
          get { return _childCommands; }
          set
          {
              _childCommands= value;
              OnPropertyChanged("ChildCommands");
          }
      }

      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }

See this for more info: http://msdn.microsoft.com/en-us/library/ms743695%28v=vs.110%29.aspx

Upvotes: 2

Related Questions