WPF:TreeView 和嵌套模板

发布于 2024-08-11 05:03:21 字数 1823 浏览 12 评论 0原文

我试图使用树视图显示一个类,但没有显示任何内容。我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阪姬 2024-08-18 05:03:21

我怀疑这与 ProjectTemplate 中嵌套的 Hier-data-templ 有关。

只是将您的源代码复制到一个空白项目中并玩了一下..这对我有用。

<Window.Resources>
      <HierarchicalDataTemplate  x:Key="ProjectItemTemplate" ItemsSource="{Binding Pages}">
         <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Foreground="Red"/>
      </HierarchicalDataTemplate>
      <HierarchicalDataTemplate x:Key="ProjectTemplate" ItemsSource="{Binding Pages}" ItemTemplate="{StaticResource ProjectItemTemplate}">
         <StackPanel>
            <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
         </StackPanel>
      </HierarchicalDataTemplate>

   </Window.Resources>
    <Grid>
      <TreeView x:Name="trePages" ItemsSource="{Binding Projects}" ItemTemplate="{StaticResource ProjectTemplate}" />
   </Grid>

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.

<Window.Resources>
      <HierarchicalDataTemplate  x:Key="ProjectItemTemplate" ItemsSource="{Binding Pages}">
         <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" Foreground="Red"/>
      </HierarchicalDataTemplate>
      <HierarchicalDataTemplate x:Key="ProjectTemplate" ItemsSource="{Binding Pages}" ItemTemplate="{StaticResource ProjectItemTemplate}">
         <StackPanel>
            <TextBlock Text="{Binding Title}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
         </StackPanel>
      </HierarchicalDataTemplate>

   </Window.Resources>
    <Grid>
      <TreeView x:Name="trePages" ItemsSource="{Binding Projects}" ItemTemplate="{StaticResource ProjectTemplate}" />
   </Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文