我的树视图定义如下:
<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>
数据绑定是:
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; }
}
我的测试绑定是:
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;
问题是,树只显示:
+ Tags
All Items
Untagged
+ Folders
dir 1
dir 2
...
Authors
Publishers
我在“所有项目”下添加的项目不会显示。
作为一个WPF小块,我无法理解这个问题。任何帮助都会受到极大关注。
答案 0 :(得分:1)
这里唯一跳出的是你在ChildTemplate中引用Children属性而不是ViewTag中定义的ChildTags。
答案 1 :(得分:0)
对于TreeView绑定,您需要使用HierarchicalDataTemplate。这允许您指定节点的数据以及节点的子节点(通过绑定到ItemsSource
)。