private ICollectionView _collectionView; private void binddata(List<obj> list) { // 创建CollectionViewSource并绑定到TreeView CollectionViewSource collectionViewSource = new CollectionViewSource { Source = list }; Treeview.ItemsSource = collectionViewSource.View; _collectionView = collectionViewSource.View; }
//搜索文本框去 private void TextBox_Changed(object sender, RoutedEventArgs e) { string filterText = Keyword.Text; // 设置过滤方法 _collectionView.Filter = item => { obj category = (obj )item; bool isfilter = false; if (category.children.Count != 0) { isfilter = category.children.Any(x => x.label.Contains(filterText)); } if (category.label.Contains(filterText)) { isfilter = true; }//多层级的使用两个if去过滤这个没过上一层可以过,这里可以做成递归 return isfilter; }; // 强制刷新视图 _collectionView.Refresh(); }
标签:category,collectionView,CollectionViewSource,ICollectionView,private,isfilter,wp From: https://www.cnblogs.com/yueyan/p/18046861