【问题标题】:How to apply filter when binding with collection inside collection in wpf using collection view source使用集合视图源在wpf中与集合内的集合绑定时如何应用过滤器
【发布时间】:2015-11-28 00:11:29
【问题描述】:

我有一个 ListBox,其扩展器与 ListBoxItem 不同。扩展器的内容中有 ListBox。现在我想在标题和扩展器的内容中应用搜索过滤器。

例如:-

如果标题有a,内容有b,c,d,bt,如果我搜索b,那么将显示扩展器有a,内部内容它只显示b和bt。

下面是我的绑定结构。

private ObservableCollection<FontDetail> _fontDetail;
public ObservableCollection<FontDetail> FontDetailList
{
    get
    {
        return _fontDetail;
    }
}

    FontDetail
    {
        public FamilyChild fontChild { get; set; }
        //ContextMenu End

        public bool IsFamily { get; set; }
        public int TotalFonts { get; set; }
        public List<FamilyChild> FamilyChildList { get; set; }
    }

    public class FamilyChild
    {
        public FontStatus Status { get; set; }


        public long TimeToAdd { get; set; }
        public FontType FontType { get; set; }
        public string SampleText { get; set; }
        public string Name { get; set; }
        public string FontFamily { get; set; }
        public string FontStyle { get; set; }
        public long FontWeight { get; set; }
        public string FontId { get; set; }

        public string MenuItem1 { get; set; }
        public string MenuItem2 { get; set; }
        public string MenuItem3 { get; set; }

        public bool IsEnableMenuItem1 { get; set; }
        public bool IsEnableMenuItem2 { get; set; }
        public bool IsEnableMenuItem3 { get; set; }
    }

所以我已经将 ListBox 与 FontDetailList 绑定了。现在我想在 FontDetail 和 FamilyChildList 上应用过滤器。 Expander header 中的 FontDetail 绑定,FamilyChildList 绑定为扩展器的内容。

【问题讨论】:

    标签: wpf filter listbox expander


    【解决方案1】:

    您必须应用过滤器两次。首先用于外部集合,然后在外部集合的过滤谓词中应用第二个过滤器。下面的代码过滤车辆名称以“S”开头的员工,并显示员工姓名和车辆列表。

    DataStore 包含 EmployeeList, Employee 包含 VehicleList

            private void BtnFilter_Click(object sender, RoutedEventArgs e)
            {
                var source = CollectionViewSource.GetDefaultView(Dgrd.ItemsSource);            
                source.Filter = new Predicate<object>(FilterEmployees);            
    
                source.Refresh();
            }
    
            private bool FilterEmployees(object o)
            {
                DB1.Employee e = o as DB1.Employee;
    
                var vehicles = CollectionViewSource.GetDefaultView(e.Vehicles);
                vehicles.Filter = new Predicate<object>(FilterVehicles);
    
                return (e.Vehicles.Where(v=>v.Name.StartsWith("S"))).Count() > 0;
            }
    
            private bool FilterVehicles(object obj)
            {
                DB1.Vehicle v = obj as DB1.Vehicle;
                return v.Name.StartsWith("S");
            }
    

    【讨论】:

    • 谢谢这真的是我想要的,它对我有用。我这里还有一个问题。
    • 我这里还有一个问题假设丹麦人只有一辆吉普车,我不想在过滤时只显示丹麦人吉普车。有可能做到吗?
    • @Prem 这是一个单独的问题。请单独询问。
    猜你喜欢
    • 2014-07-11
    • 1970-01-01
    • 2013-04-11
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 2020-11-24
    相关资源
    最近更新 更多