WPF分层数据模板过滤器

时间:2019-05-07 09:14:58

标签: c# wpf

我在ViewModel

public string SearchPattern
{
    get
    {
        return searchPattern;
    }

    set
    {
        searchPattern = value;
    }
}

private bool UserFilter(object item)
{
    return (item as Node).Name.Contains(SearchPattern);
}

public ICollectionView YourFilteredCollection
{
    get
    {
        var source = CollectionViewSource.GetDefaultView(model.Items);
        source.Filter = UserFilter;
        return source;
    }
}

YourFilteredCollection取决于Model属性

 public ObservableCollection<Node> Items { get; set; }

比我决定过滤Node子项YourFilteredCollectionNodes

public class Node
{
    public string Name { get; set; }

    public ObservableCollection<Node> Nodes { get; set; }

    public ICollectionView YourFilteredCollectionNodes
    {
        get
        {
            var source = CollectionViewSource.GetDefaultView(Nodes);
            if (source != null)
            {
                source.Filter = UserFilter;
            }
            return source;
        }
    }

    private bool UserFilter(object item)
    {
        //return (item as Node).Name.Contains(SearchPattern);
        return true;
    }
}

但是在这种情况下,如何从ViewModel中的SearchPattern项的ViewModel中传递ObservableCollection

0 个答案:

没有答案