【发布时间】:2014-03-25 20:07:56
【问题描述】:
我有一个定义自定义 GetEnumerator() 函数的类(通过实现 IEnumerable)。我用它以连续的方式迭代每个 TestStep 中的几个ObservableCollection<LogEvent>。我有一个私人的ObservableCollection<TestStep>,其中包含所有需要的数据。
我想使用这个类的一个实例作为 ListBox 的 ItemsSource。但是,当基础数据 (ObservableCollection<LogEvent>) 更新时,ListBox 永远不会更新。这是该类的示例:
public class FlatLogViewModel : IEnumerable<LogEvent>
{
public FlatLogViewModel(ObservableCollection<TestStep> subSteps)
{
m_subSteps = subSteps;
}
public IEnumerator<LogEvent> GetEnumerator()
{
foreach (TestStep step in SubSteps)
{
// step.LogEvents is an ObservableCollection<LogEvent>
foreach (LogEvent logEvent in step.LogEvents)
yield return logEvent;
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
private ObservableCollection<TestStep> m_subSteps;
}
我不确定我是否应该/可以在此处实施 INotifyCollectionChanged。如何知道 ObservableCollection 是否被修改?
我的问题是:如何让 ListBox 显示 LogEvents 中发生的变化(类型为 ObservableCollection<LogEvent>)?
【问题讨论】:
标签: c# wpf listbox enumerator inotifycollectionchanged