我在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