简体   繁体   English

WPF:TreeView数据绑定问题

[英]WPF: Problem with TreeView databinding

I have a tree view defined as follows: 我有一个树形视图,定义如下:

<HierarchicalDataTemplate x:Key="ChildTemplate"
                          ItemsSource="{Binding Children}">
    <TextBlock Text="{Binding TagName, Mode=OneWay}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="NavigationHeaderTemplate"
                          ItemsSource="{Binding Children}"
                          ItemTemplate="{StaticResource ChildTemplate}">
    <StackPanel Orientation="Horizontal" Margin="0">
        <Image Source="{Binding Image}" Height="16" Width="16"></Image>
        <TextBlock Text="{Binding Header}"></TextBlock>
    </StackPanel>
</HierarchicalDataTemplate>
<TreeView Grid.Row="0" Grid.Column="0" Margin="0"
    FlowDirection="LeftToRight"
    ItemTemplate="{StaticResource NavigationHeaderTemplate}"
    Name="TreeView2">
</TreeView>

The data binding is: 数据绑定为:

public class ViewTag : INotifyPropertyChanged
{
    private string _tagName;
    public string TagName
    {
        get { return _tagName; }
        set
        {
            _tagName = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Tag Name"));
        }
    }
    private ObservableCollection<ViewTag> _childTags;
    public ObservableCollection<ViewTag> ChildTags
    {
        get { return _childTags; }
        set
        {
            _childTags = value;
            OnPropertyChanged(new PropertyChangedEventArgs("Child Tags"));
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }
    #endregion

    public ViewTag(string tagName, ObservableCollection<ViewTag> childTags)
    {
        _tagName = tagName;
        _childTags = childTags;
    }
}

public class ViewNavigationTree
{
    public string Image { get; set; }
    public string Header { get; set; }
    public ObservableCollection<ViewTag> Children { get; set; }
}

And my test binding is: 我的测试绑定是:

        var xy = new List<ViewNavigationTree>();
        List<ViewTag> tempTags = new List<ViewTag>();
        ViewTag t1, t2, t3, t4, t5, t6;
        t1 = new ViewTag("Computers", null);
        t2 = new ViewTag("Chemistry", null);
        t3 = new ViewTag("Physics", null);
        var t123 = new ObservableCollection<ViewTag>();
        t123.Add(t1);
        t123.Add(t2);
        t123.Add(t3);
        t4 = new ViewTag("Science", t123);
        var t1234 = new ObservableCollection<ViewTag>();
        t1234.Add(t4);
        t5 = new ViewTag("All Items", t1234);
        t6 = new ViewTag("Untagged", null);

        var tall = new ObservableCollection<ViewTag>();
        tall.Add(t5);
        tall.Add(t6);
        xy.Add(new ViewNavigationTree() { Header = "Tags", Image = "img/tags2.ico", Children = tall });

        var rootFolders = eDataAccessLayer.RepositoryFacrory.Instance.MonitoredDirectoriesRepository.Directories.ToList();
        var viewFolders = new ObservableCollection<ViewTag>();
        foreach (var vf in rootFolders)
        {
            viewFolders.Add(new ViewTag(vf.FullPath, null));
        }
        xy.Add(new ViewNavigationTree() { Header = "Folders", Image = "img/folder_16x16.png", Children = viewFolders });

        xy.Add(new ViewNavigationTree() { Header = "Authors", Image = "img/user_16x16.png", Children = null });
        xy.Add(new ViewNavigationTree() { Header = "Publishers", Image = "img/powerplant_32.png", Children = null });

        TreeView2.ItemsSource = xy;

Problem is, the tree only shows: 问题是,树只显示:

+ Tags
    All Items
    Untagged
+ Folders
    dir 1
    dir 2
    ...
Authors
Publishers

The items I added under "All Items" aren't displayed. 我在“所有项目”下添加的项目不会显示。

Being a WPF nub, i can't put my finger on the problem. 作为WPF的小子,我不能专心解决这个问题。 Any help will be greatly appriciated. 任何帮助将不胜感激。

唯一跳到这里的是您要引用ChildTemplate中的Children属性,而不是ViewTag中定义的ChildTags。

For TreeView binding, you need to use a HierarchicalDataTemplate . 对于TreeView绑定,您需要使用HierarchicalDataTemplate This allows you to specify both the data for the node, and also the children for the node (by binding to ItemsSource ). 这使您既可以指定该节点的数据,又可以指定该节点的子级(通过绑定到ItemsSource )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM