【问题标题】:Why is my converter (based on ListView.ItemsSource) not being called?为什么我的转换器(基于 ListView.ItemsSource)没有被调用?
【发布时间】:2012-02-11 21:27:36
【问题描述】:

我有一个小型 WPF 应用程序,其中有一个文件列表。在文件列表下方,我有一个“上传”按钮。我希望上传按钮中的文本显示“上传 x 个文件”,其中 x 是列表中的项目数。

我正在使用一个转换器,它将 ItemsSource 属性作为输入并返回字符串,但是如果我从列表视图(即它的基础集合)中添加或删除项目,则不会调用按钮绑定上的转换器。

我做错了什么?

我有基类:

public class FileItem : INotifyPropertyChanged { ... }

还有一个 ObservableCollection:

public class Files : ObservableCollection<FileItem> {}

我将集合分配给 ListView。

lvw_FileList.ItemsSource = new Files();

ListView 绑定了它的 ItemSource 属性。

<ListView x:Name="lvw_FileList" ItemsSource="{Binding Mode=OneWay}">
...
</ListView>

文本内容需要根据ListView.ItemsSource进行转换器的按钮。

<Button Content="{Binding ElementName=lvw_FileList, Path=ItemsSource, Converter={StaticResource UpdateButtonConverter}}" x:Name="btn_Upload" /> 

【问题讨论】:

  • ListView.ItemsSource 的绑定没用,你用代码中的赋值覆盖它。

标签: c# wpf xaml data-binding


【解决方案1】:

绑定只听PropertyChanged 而不是CollectionChanged 通知。如果您需要在集合更改时运行转换,请考虑为集合及其Count 属性添加MultiBinding。计数更改将触发重新评估,并且可以在转换器中使用集合值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-11
    • 2021-03-30
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多