XAML - 如何为分层数据

时间:2018-04-16 06:36:56

标签: c# wpf xaml mvvm collectionviewsource

我有一个具有分层数据的树视图。我想为它添加一个过滤器,所以我可以搜索任何元素到n级。为此,我发现嵌套collectionviewsource的概念很有趣。但我不确定它将如何以编程方式工作。

假设我们有一个的集合,每个组都有一个元素的集合。那么,我如何编写嵌套的collectionviewsource所以,我可以过滤所有组中所有元素的搜索结果?

以下是我认为应该可以使用的示例代码,但是它只会过滤第一级的数据而不会搜索“元素”。所以,我想知道如何编写一个正确的嵌套collectionviewsource来过滤树视图中的分层数据。

<UserControl.Resources>
    <CollectionViewSource x:Key="CollectionViewSourceGroups" x:Name="_CollectionViewSourceGroups" Source="{Binding Groups}" Filter="_filterGroups"/>
    <CollectionViewSource x:Key="CollectionViewSourceElements" x:Name="_CollectionViewSourceElements"  Source="{Binding Elements, Source={StaticResource CollectionViewSourceGroups}}" Filter="_filterElements"/>
</UserControl.Resources>

这是触发过滤器命令的代码:

var cvs = TryFindResource("CollectionViewSourceGroups") as CollectionViewSource;
if (cvs != null)
{
    if (cvs.View != null)
        cvs.View.Refresh();
}
cvs = TryFindResource("CollectionViewSourceElements") as CollectionViewSource;
if (cvs != null)
{
    if (cvs.View != null)
        cvs.View.Refresh();
}

修改 以下是MVVM和XAML代码示例:

/// <summary>
/// Group of Views
/// </summary>
public class ViewGroup : INotifyPropertyChanged
{
    public string Name { get; set; }
    public List<ViewGroup> Groups { get; set; }


    private List<ProjectObject> _elements;
    public List<ProjectObject> Elements
    {
        get { return _elements; }
        set
        {
            _elements = value;
            OnPropertyChanged();

        }
    }

    public ViewGroup()
    {
        Groups = new List<ViewGroup>();
        Elements = new List<ProjectObject>();
    }

    public event PropertyChangedEventHandler PropertyChanged;
    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}
public class SheetManagerViewModel
{
    public static SheetManagerViewModel Instance;      
    public ObservableCollection<ViewGroup> Groups { get; set; }

    public SheetManagerViewModel()
    {
        Instance = this;
        Groups = new ObservableCollection<ViewGroup>();            
    }
}
<Grid Grid.Row="1">
    <Grid>
        <TreeView  x:Name="ProjectBrowserTreeView" ItemsSource="{Binding Groups}"  MouseMove="ProjectBrowserTreeView_OnMouseMove" DragOver="ProjectBrowserTreeView_OnDragOver" >

            <TreeView.ItemTemplate>
                <HierarchicalDataTemplate ItemsSource="{Binding Groups}" DataType="{x:Type local:ViewGroup}">
                    <Label Content="{Binding Name}"/>
                    <HierarchicalDataTemplate.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Elements}" DataType="{x:Type local:ViewGroup}">
                            <Label Content="{Binding Name}"/>
                            <HierarchicalDataTemplate.ItemTemplate>
                                <DataTemplate DataType="{x:Type local:ProjectObject}">
                                    <Label Content="{Binding Name}"/>

                                </DataTemplate>
                            </HierarchicalDataTemplate.ItemTemplate>
                        </HierarchicalDataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Grid>
</Grid>

0 个答案:

没有答案