我在XAML定义为列表框:WPF列表框不重绘
<ListBox x:Name="directoryList"
MinHeight="100"
Grid.Row="0"
ItemsSource="{Binding Path=SelectedDirectories}"/>
的SelectedDirectories上类型的列表的DataContext List<DirectoryInfo>
的类,这是一个属性为列表框的datacontext实现INotifyPropertyChanged 。当收集更改项目成功添加到列表但显示不更新,直到我强迫列表框中通过调整重新绘制。
任何想法为什么?
编辑:INotifyPropertyChanged的实施
public class FileScannerPresenter : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private FileScanner _FileScanner;
public FileScannerPresenter()
{
this._FileScanner = new FileScanner();
}
public List<DirectoryInfo> SelectedDirectories
{
get
{
return _FileScanner.Directories;
}
}
public void AddDirectory(string path)
{
this._FileScanner.AddDirectory(path);
OnPropertyChanged("SelectedDirectories");
}
public void OnPropertyChanged(string property)
{
if (this.PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(property));
}
}
}
你说得对w.r.t.为什么我以前的代码段工作。更新。 +1 – Gishu 2009-05-27 05:58:30
我最终做的是将集合抽象到ObservableCollection中的一个演示者类中,然后只在需要时将其传递给scanner类。 – benPearce 2009-06-05 06:06:53