【问题标题】:How to get number of items in ObservableCollection from XAML?如何从 XAML 获取 ObservableCollection 中的项目数?
【发布时间】:2009-06-18 12:50:54
【问题描述】:

我正在显示从 ComboBox 中的 ViewModel ObservableCollectoin 属性获得的所有客户,如下所示:

<ComboBox 
    ItemsSource="{Binding Customers}"
    ItemTemplate="{StaticResource CustomerComboBoxTemplate}"
    Margin="20"
    HorizontalAlignment="Left"
    SelectedItem="{Binding SelectedCustomer, Mode=TwoWay}"/>

有没有办法在不创建另一个 ViewModel 属性的情况下获取 ObservableCollection 中的项目数,例如像这样:

伪代码:

<TextBlock Text="{Binding Customers.Count()}"/>

【问题讨论】:

    标签: wpf xaml observablecollection


    【解决方案1】:

    ObservableCollection 类型公开了一个您可以使用的 Count 属性。 我不知道 ObservableCollection 是否会引发 PropertyChanged 事件以通知 UI 有关此属性的更新。

    【讨论】:

    • 好吧,就是这样:,是的,当 ObservableCollection 发生变化时,它似乎会不断更新,很好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2012-11-14
    相关资源
    最近更新 更多