[英]Why is my converter (based on ListView.ItemsSource) not being called?
I have a small WPF application in which I have a list of files. 我有一个小的WPF应用程序,其中有一个文件列表。 Below the list of files I have an "Upload" button. 在文件列表下方,我有一个“上传”按钮。 I would like the text in the upload button to display "Upload x files" where x is the number of items in the list. 我希望上传按钮中的文本显示“上传x文件”,其中x是列表中的项目数。
I am using a converter which takes the ItemsSource property as input and returns the string, however if I add or remove items from the listview (ie. it's underlying collection) the converter on the button binding is not called. 我正在使用一个转换器,它将ItemsSource属性作为输入并返回字符串,但是如果我在listview中添加或删除项目(即它的底层集合),则不会调用按钮绑定上的转换器。
What am I doing incorrectly? 我做错了什么?
I have the base class: 我有基类:
public class FileItem : INotifyPropertyChanged { ... }
And an ObservableCollection: 还有一个ObservableCollection:
public class Files : ObservableCollection<FileItem> {}
An I assign the collection to the ListView. 我将集合分配给ListView。
lvw_FileList.ItemsSource = new Files();
The ListView has it's ItemSource property bound. ListView具有绑定的ItemSource属性。
<ListView x:Name="lvw_FileList" ItemsSource="{Binding Mode=OneWay}">
...
</ListView>
The button which text content needs a converter based on the ListView.ItemsSource. 文本内容需要基于ListView.ItemsSource的转换器的按钮。
<Button Content="{Binding ElementName=lvw_FileList, Path=ItemsSource, Converter={StaticResource UpdateButtonConverter}}" x:Name="btn_Upload" />
Bindings only listen to PropertyChanged
and not CollectionChanged
notifications. 绑定仅侦听PropertyChanged
而不是CollectionChanged
通知。 If you need to run the conversion whenever the collection changes consider a MultiBinding
to both the collection and its Count
property. 如果需要在集合更改时运行转换,请考虑对集合及其Count
属性进行MultiBinding
。 The count change will trigger a reevaluation and the collection value can be used in the converter. 计数更改将触发重新评估,并且可以在转换器中使用收集值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.