绑定到列表计数,其中 Typeof

发布于 2024-12-08 19:10:30 字数 460 浏览 0 评论 0原文

我知道如何绑定到计数,但是如果我只想要类型为 Product 的计数

<TextBlock Text="{Binding Items.Count}" />
Items = new ObservableCollection<object>();

,我尝试使用属性,但我在添加或删除项目时遇到保持同步的问题。

    public int ProductCount
            {
                get
                {
                    return Items.Cast<object>().Count(item => item.GetType() == typeof (ProductViewModel));
                }
            }

I know how to Bind to Count, but how do I do it, if I only want the count where type is Product

<TextBlock Text="{Binding Items.Count}" />
Items = new ObservableCollection<object>();

I tried with a Property, But I am having problem keeping it in sync when Items are being added or removed.

    public int ProductCount
            {
                get
                {
                    return Items.Cast<object>().Count(item => item.GetType() == typeof (ProductViewModel));
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小瓶盖 2024-12-15 19:10:30

使用 LINQ OfType() 您可以在 ProductCount 属性 getter:

return Items.OfType<ProductViewModel>().Count();

顺便说一句,为了更安全,请使用以下空检查条件:

return Items == null ? 0 : Items.OfType<ProductViewModel>().Count();

BTW2,避免使用 Cast<> 在这种情况下,因为它会抛出 InvalidCastException 无效转换操作的情况下的异常。

Using LINQ OfType() you can return value of following statement in ProductCount property getter:

return Items.OfType<ProductViewModel>().Count();

BTW, to be more safe use following null-check condition:

return Items == null ? 0 : Items.OfType<ProductViewModel>().Count();

BTW2, avoid using of the Cast<> in such cases because it throws the InvalidCastException exception in case of invalid cast operation.

吲‖鸣 2024-12-15 19:10:30

除了获取与类型匹配的正确数量的项目之外,您还必须保证在项目集合更改时引发视图模型的 PropertyChanged 事件。所以基本上你需要的是这样的:

class ProductViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private ObservableCollection<object> m_Items;
    public ObservableCollection<object> Items
    {
        get { return m_Items; }
        set 
        { 
            if(m_Items != null)
                m_Items.CollectionChanged -= HandleItemsCollectionChanged;

            m_Items = value; 
            m_Items.CollectionChanged += HandleItemsCollectionChanged; 

            PropertyChanged(this, new PropertyChangedEventArgs("Items");
        }
    }

    public int ProductCount
    {
        get
        {
            return Items == null 
                ? 0 
                : Items.OfType<ProductViewModel>().Count();
        }
    }

    private void HandleItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        PropertyChanged(this, new PropertyChangedEventArgs("ProductCount");
    }
}

Additional to getting the correct number of items matching the type you have to guarantee that the PropertyChanged event of the view model is raised when the item collection is changed. So basically what you need is something like this:

class ProductViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    private ObservableCollection<object> m_Items;
    public ObservableCollection<object> Items
    {
        get { return m_Items; }
        set 
        { 
            if(m_Items != null)
                m_Items.CollectionChanged -= HandleItemsCollectionChanged;

            m_Items = value; 
            m_Items.CollectionChanged += HandleItemsCollectionChanged; 

            PropertyChanged(this, new PropertyChangedEventArgs("Items");
        }
    }

    public int ProductCount
    {
        get
        {
            return Items == null 
                ? 0 
                : Items.OfType<ProductViewModel>().Count();
        }
    }

    private void HandleItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        PropertyChanged(this, new PropertyChangedEventArgs("ProductCount");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文