所以我有一些基本的东西
private ObservableCollection<ViewModel> _internal;
public ObservableCollection<ViewModel> BoundInternal{get;set}; //this is Binded in the Itemssource like ItemSource={Binding BoundInternal}
现在在我的代码中我做了类似
的事情BoundInternal = _internal,但问题是BoundInternal不会触发任何collectionChanged事件。我必须使用Add方法。所以我想知道是否有解决方案。
答案 0 :(得分:4)
我怀疑你的代码看起来应该是这样的(虽然它与你目前所做的不太匹配): -
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));
}
}
在这种情况下,_internal
字段直接成为BoundInternal
值的来源,您只能通过BoundInternal
分配,(不要直接为{_internal
分配值1}})。当发生这种情况时,任何与其相关的东西都会被告知变化。
如果由于某种原因,您确实需要将_internal
作为BoundInternal
的支持字段的单独参考,那么: -
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));
}
}
现在,当您执行BoundInternal = _internal
时,代码中的某些位置会被告知此更改。
答案 1 :(得分:2)
每个ItemsControl都有一个Items
属性,您可以使用Refresh()
方法调用该属性,这将更新您的列表。
MyList.Items.Refresh()