WPF:TreeView 和嵌套模板
我试图使用树视图显示一个类,但没有显示任何内容。我用 Snoop 检查了容器,看到了 StackPanel,但它是空的,即使项目确实有标题和页面。没有绑定错误,我不知道为什么它不起作用。
public class Project : INotifyPropertyChanged {
public string Title {get;set;}
public ObservableCollection<InfoPage> Pages {get;set;}
...
public class InfoPage : INotifyPropertyChanged {
public string Title {get;set;}
public ObservableCollection<InfoPage> Pages {get;set;}
...
<HierarchicalDataTemplate x:Key="ProjectPageTemplate" ItemsSource="{Binding Pages}">
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="ProjectTemplate" ItemsSource="{Binding Pages}">
<StackPanel>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
然后在窗口本身上:
public static DependencyProperty ProjectProperty = DependencyProperty.Register("Project", typeof(Project), typeof(WinMain));
public Project Project {
get { return base.GetValue(ProjectProperty) as Project; }
set { base.SetValue(ProjectProperty, value); }
}
<TreeView x:Name="trePages" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding Project,ElementName=root,Converter={StaticResource dbg},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource ProjectTemplate}"/>
I'm trying to display a class using a treeview but nothing is displayed. I've inspected the container with Snoop and I see the StackPanel, but it's empty, even the the project does have a title and pages. There are no binding errors and I have no idea why it's not working.
public class Project : INotifyPropertyChanged {
public string Title {get;set;}
public ObservableCollection<InfoPage> Pages {get;set;}
...
public class InfoPage : INotifyPropertyChanged {
public string Title {get;set;}
public ObservableCollection<InfoPage> Pages {get;set;}
...
<HierarchicalDataTemplate x:Key="ProjectPageTemplate" ItemsSource="{Binding Pages}">
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="ProjectTemplate" ItemsSource="{Binding Pages}">
<StackPanel>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate>
<TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
Then on the Window itself:
public static DependencyProperty ProjectProperty = DependencyProperty.Register("Project", typeof(Project), typeof(WinMain));
public Project Project {
get { return base.GetValue(ProjectProperty) as Project; }
set { base.SetValue(ProjectProperty, value); }
}
<TreeView x:Name="trePages" Grid.Row="2" Grid.Column="0" ItemsSource="{Binding Project,ElementName=root,Converter={StaticResource dbg},Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemTemplate="{StaticResource ProjectTemplate}"/>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑这与 ProjectTemplate 中嵌套的 Hier-data-templ 有关。
只是将您的源代码复制到一个空白项目中并玩了一下..这对我有用。
I suspect it's got something to do with the nested Hier-data-templ in ProjectTemplate.
Just copied your source into a blank project and toyed a bit.. Here's what worked for me.