0
更新請參閱Simplifying the WPF TreeView ...瞭解此實施。這個TreeView和Hierarchical數據綁定有什麼問題?
從底部:
- 鹼(摘要)類
Folder
在AssemblyA - 子類
Folder
S IN AssemblyB:BuildFolder
,Solution
,Project
- 樹視圖在AssemblyC試圖綁定到分層文件夾結構
我有視圖模型ProjectExplorerVM
封裝BuildItemVM
。 BuildItemVM
迭代的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控件中。我的綁定有什麼問題?
有什麼問題? –
一個簡單的評論會引起我的注意......不需要反對投票。 :) – IAbstract
不是我,我不是那個意思;) –