我在 XAML 中定义了一个列表框:
<ListBox x:Name="directoryList"
MinHeight="100"
Grid.Row="0"
ItemsSource="{Binding Path=SelectedDirectories}"/>
SelectedDirectories 是列表 DataContext 类型的属性List<DirectoryInfo>
作为列表框数据上下文的类实现了 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));
}
}
}