WPF 列表框不重绘
我在 XAML 中定义了一个列表框:
<ListBox x:Name="directoryList"
MinHeight="100"
Grid.Row="0"
ItemsSource="{Binding Path=SelectedDirectories}"/>
SelectedDirectories 是列表 DataContext 类型 List
上的一个属性 作为
列表框数据上下文的类实现了 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));
}
}
}
I have a listbox defined in XAML as:
<ListBox x:Name="directoryList"
MinHeight="100"
Grid.Row="0"
ItemsSource="{Binding Path=SelectedDirectories}"/>
The SelectedDirectories is a property on the lists DataContext of type List<DirectoryInfo>
The class which is the datacontext for the listbox implements INotifyPropertyChanged. When the collection changes the items are added successfully to the list however the display does not update until I force the listbox to redraw by resizing it.
Any ideas why?
EDIT: INotifyPropertyChanged implementation
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));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
相反 - 您无缘无故地触发整个列表框的刷新,并且您不需要使您的托管类实现 INotifyPropertyChanged - 它可以很容易地只是窗口的属性。 关键是永远不要将该属性设置为新实例。 所以:
如果您最终使用 FileScanner 类,则需要实现 INotifyCollectionChanged - 这样,ListBox 就知道要动态添加/删除什么。
Try
instead - you're triggering a refresh of the entire ListBox for no reason, and you don't need to make your hosting class implement INotifyPropertyChanged - it could easily just be a property of the window. The key is to never set the property to a new instance. So:
If you end up using that FileScanner class, you need to implement INotifyCollectionChanged instead - that way, the ListBox knows what to add/remove dynamically.
(请参阅下面的更新)。 WPF 似乎工作正常。 我将你的代码放入一个新项目中。 每当我单击按钮调用 AddDirectory 时,列表框就会更新。 您不需要再更改任何代码。
问题似乎是别的问题。您的 UI 中是否有多个线程?
我没有 FileScanner 类型。 所以我创建了一个虚拟对象,如下所示。
您的 FileScannerPresenter 类没有更改。 或者您的列表框 XAML。 我创建了一个带有 DockPanel 的窗口,其中包含列表框、文本框和按钮。
更新:保罗·贝茨是对的。 它之所以有效,是因为我每次都从 Bound 属性返回一个新列表。 与列表的数据绑定总是让我感到困惑。
经过更多修改,最简单的方法是:
ObservableCollection
(它为您实现INotifyCollectionChanged
)。 一直更改所有签名以返回此类型,而不是List
FileScanner 和 FileScannerPresenter 本身不必实现任何 INotifyXXX 接口。
(See Update below). WPF seems to be working alright. I put your code into a new project. The listbox updates whenever I click the button to invoke AddDirectory. You should not need any more code changes.
The problem seems to be something else.. Are there multiple threads in your UI?
I didnt have the FileScanner type. So I created a dummy as follows.
No changes to your FileScannerPresenter class. Or your listbox XAML. I created a Window with a DockPanel containing your listbox, a textbox and a button.
Update: Paul Betts is right. It works because I return a new list each time from the Bound property. Data binding with lists always messes me up.
With more tinkering, the easy way to do this is:
ObservableCollection<DirectoryInfo>
(which implementsINotifyCollectionChanged
for you). Change all signatures all the way up to return this type instead of aList<DirectoryInfo>
FileScanner and FileScannerPresenter themselves do not have to implement any INotifyXXX interface.