WPF-ObservableCollection 的多个视图

发布于 2024-11-24 01:54:21 字数 177 浏览 2 评论 0原文

我有一个包含两个定义的 CollectionView 的视图模型。

我用它来导航和数据输入/编辑。 另一个我想用于过滤目的并在表单上的某些列表视图中显示过滤。

当我对 observablecollection 应用过滤时,我不希望主视图(用于 DataEntry 目的)受到影响。

提前致谢!

I have a viewmodel containing two CollectionViews defined.

One I am using for navigation and data entry/edit.
Another I want to use for filtering purpose and show the filteration in some Listview on the form.

I don't want the main view(used for DataEntry purpose) to get affected while I applying filteration on observablecollection.

Thanks in Advance!

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

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

发布评论

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

评论(2

孤云独去闲 2024-12-01 01:54:21

只要您使用单独的集合视图,更改一个集合视图就不会影响另一个集合视图。这就是集合视图的要点 - 它们是同一集合的独立视图。

As long as you're using separate collection views, changing one won't affect the other. That is the point of collection views - they're independent views on the same collection.

蹲在坟头点根烟 2024-12-01 01:54:21

好的,明白了!并以同样的想法继续前进。但是当我这样做时,我得到错误=“调用线程无法访问该对象,因为另一个线程拥有它。”。因此我的过滤不起作用..以下是代码-

    public ICollectionView Clients { get; set; } //Used for Data-navigation/modification
    public ListCollectionView CodeView { get; set; } // to be used for filteration purpose on form.

    string searchText = String.Empty;
    public string CompanyCodeSearch
    {
        get { return searchText; }
        set
        {
            try
            {
                searchText = value;
                OnPropertyChanged("CompanyCodeSearch");
                CodeView.Filter = new Predicate<object>(cmFilterData);
            }
            catch (Exception ex)
            {

            }
        }
    }


   private bool cmFilterData(object item)
    {
        bool _filteredData = false;
        try
        {
            var value = (item as cntClient);
            if (value == null || value.CompanyCode == null)
                return false; 

            _filteredData = value.CompanyCode.StartsWith(this.CompanyCodeSearch);
            return _filteredData;
        }
        catch (Exception ex)
        {
            return false; 
        }
    }

ok, Got it! and went ahead with the same idea. But when I did so, I get Error = "The calling thread cannot access this object because a different thread owns it.". Hence my filteration doesn't work.. Following is the code-

    public ICollectionView Clients { get; set; } //Used for Data-navigation/modification
    public ListCollectionView CodeView { get; set; } // to be used for filteration purpose on form.

    string searchText = String.Empty;
    public string CompanyCodeSearch
    {
        get { return searchText; }
        set
        {
            try
            {
                searchText = value;
                OnPropertyChanged("CompanyCodeSearch");
                CodeView.Filter = new Predicate<object>(cmFilterData);
            }
            catch (Exception ex)
            {

            }
        }
    }


   private bool cmFilterData(object item)
    {
        bool _filteredData = false;
        try
        {
            var value = (item as cntClient);
            if (value == null || value.CompanyCode == null)
                return false; 

            _filteredData = value.CompanyCode.StartsWith(this.CompanyCodeSearch);
            return _filteredData;
        }
        catch (Exception ex)
        {
            return false; 
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文