开发者

Having ObservableCollection Trigger an update

So i have something along the lines of

private  ObservableCollection<ViewModel> _internal;

public ObservableCollection<ViewModel> BoundInternal{get;set}; //this is Binded in the Itemssource like ItemSource={Binding BoundInternal}

Now In my code i do something like

BoundInternal=_internal, However the problem is the BoundInternal isn't trigger any collectionChanged event. I have to use the Add method. So I am wondering if there开发者_高级运维 is a solution to this.


Here is what I suspect your code ought to look like like (although its not quite a match for what you currently doing):-

public class YourClassHoldingThisStuff : INotifyProperyChanged
{
  private  ObservableCollection<ViewModel> _internal;

  public ObservableCollection<ViewModel> BoundInternal
  {
    get { return _internal; }
    set
    {
      _internal = value;
      NotifyPropertyChanged("BoundInternal");
    };
  }
  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(string name)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, new ProperytChangedEventArgs(name));
  }
}

In this case the _internal field becomes the source of the value of BoundInternal directly and you should only assign it via BoundInternal, (don't assign a value directly to _internal). When that occurs anything currently bound to it will be informed of the change.

If for some reason you really do need to maintain _internal as a separate reference from the backing field of BoundInternal then:-

public class YourClassHoldingThisStuff : INotifyProperyChanged
{
  private  ObservableCollection<ViewModel> _internal;
  private  ObservableCollection<ViewModel> _boundInternal;

  public ObservableCollection<ViewModel> BoundInternal
  {
    get { return _boundInternal; }
    set
    {
      _boundInternal = value;
      NotifyPropertyChanged("BoundInternal");
    };
  }
  public event PropertyChangedEventHandler PropertyChanged;

  private void NotifyPropertyChanged(string name)
  {
    if (PropertyChanged != null)
      PropertyChanged(this, new ProperytChangedEventArgs(name));
  }
}

Now at some point in your code when you do BoundInternal = _internal, anything bound to it will be informed of the change.


Every ItemsControl has a, Items property which has a Refresh() method that you can call, which will update your list.

MyList.Items.Refresh()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜