这个TreeView和Hierarchical数据绑定有什么问题?

时间:2016-03-24 18:22:15

标签: c# wpf data-binding treeview hierarchical-data

更新有关此实施的见解,请参阅Simplifying the WPF TreeView ...

从底部开始:

    Assembly中的
  • base(abstract)class Folder
  • Assembly中的
  • 子类FolderBuildFolderSolutionProject
  • AssemblyC中的TreeView正在尝试绑定到分层文件夹结构

我有一个封装ProjectExplorerVM的视图模型BuildItemVMBuildItemVM迭代Folder s的嵌套结构:

public event PropertyChangedEventHandler PropertyChanged;

public ObservableCollection<BuildItemVM> Folders {
    get { return folders; }
}

#region constructor
public BuildItemVM(Build Build) : this(Build.RootFolder, null) {
    Name = Build.Name;
}

private BuildItemVM(Folder childFolder, BuildItemVM parentFolder) {
    folderItem = childFolder;
    parentItem = parentFolder;
    Name = folderItem.Name;

    folders = new ObservableCollection<BuildItemVM>(childFolder.Folders
        .Select(f => new BuildItemVM(f, this)));
    folders.CollectionChanged += FoldersCollectionChanged;
}
#endregion

ProjectExplorerVM如下:

    #region properties

    public event PropertyChangedEventHandler PropertyChanged;


    public ReadOnlyCollection<BuildItemVM> RootItems {
        get;
        private set;
    }
    #endregion


    #region constructor
    public ProjectExplorerVM(Build BuildProject) {
        build = BuildProject;
        buildItemVM = new BuildItemVM(build);

        buildItemVM.Folders.CollectionChanged += FoldersCollectionChanged;

        RootItems = new ReadOnlyCollection<BuildItemVM>(new BuildItemVM[] { buildItemVM });
    }
    #endregion

...好吧,所以我尝试了很多不同的东西,我在谷歌机器上找到了。以下是我在XAML中进行绑定尝试的内容:

    <TreeView Name="BuildTree" 
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch">
        <TreeView.Resources>
            <build:BuildFolder x:Key="FolderList"/>

            <HierarchicalDataTemplate DataType="{x:Type build:BuildFolder}"
                                      ItemsSource="{Binding Path=Folders}">
                <TextBlock Text="{Binding Path=Name}"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type build:Project}"
                                      ItemsSource="{Binding Path=Folders}">
                <TextBlock Text="{Binding Path=Name}"/>
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type build:Solution}"
                                      ItemsSource="{Binding Path=Folders}">
                <TextBlock Text="{Binding Path=Name}"/>
            </HierarchicalDataTemplate>
        </TreeView.Resources>
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                <Setter Property="FontWeight" Value="Normal" />
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="FontWeight" Value="Bold" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </TreeView.ItemContainerStyle>
        <TreeViewItem ItemsSource="{Binding Source={StaticResource FolderList}}"/>
    </TreeView>

TreeView控件中没有节点。我的绑定有什么问题?

1 个答案:

答案 0 :(得分:0)

原来我在绑定中使用了不正确的属性名称。所以我发布了正确的绑定(Xaml):

                <TreeView Name="BuildTree"
                          ItemsSource="{Binding RootItems}">
                    <TreeView.ItemContainerStyle>
                        <Style TargetType="{x:Type TreeViewItem}">
                            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                            <Setter Property="FontWeight" Value="Normal" />
                            <Style.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter Property="FontWeight" Value="Bold" />
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </TreeView.ItemContainerStyle>

                    <TreeView.ItemTemplate>
                        <HierarchicalDataTemplate ItemsSource="{Binding Folders}">
                            <TextBlock Text="{Binding Name}" />
                        </HierarchicalDataTemplate>
                    </TreeView.ItemTemplate>
                </TreeView>