2016-03-24 76 views
0

更新請參閱Simplifying the WPF TreeView ...瞭解此實施。這個TreeView和Hierarchical數據綁定有什麼問題?

從底部:

  • 鹼(摘要)類Folder在AssemblyA
  • 子類Folder S IN AssemblyB:BuildFolderSolutionProject
  • 樹視圖在AssemblyC試圖綁定到分層文件夾結構

我有視圖模型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

一個簡單的評論會引起我的注意......不需要反對投票。 :) – IAbstract

+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>