【问题标题】:WPF - I'm stumped on the TreeView controlWPF - 我对 TreeView 控件感到困惑
【发布时间】:2014-02-11 22:50:36
【问题描述】:

双关语。

我想使用 HierarchicalDataTemplate 类创建一个简单的 TreeView。

这是我的问题 XAML:

<Window.Resources>
    <ObjectDataProvider 
        x:Key="myDataProvider"
        ObjectType="vm:ContractViewModel" />
</Window.Resources>

<Window.DataContext>
    <Binding Source="{StaticResource myDataProvider}" Path="Contract" />
</Window.DataContext>

<StackPanel
    Orientation="Vertical"
    VerticalAlignment="Top">
    <ListBox MinWidth="400" Margin="10"
        ItemsSource="{Binding Commissions}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <TextBlock Text="{Binding Id}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <TreeView>
        <HierarchicalDataTemplate DataType="{x:Type m:Contract}"
                                    ItemsSource="{Binding Commissions}">
            <TextBlock Text="{Binding Id}" />
        </HierarchicalDataTemplate>
    </TreeView>
</StackPanel>

我正在使用 MVVM 模式。 StaticResource“myDataProvider”返回一个 Contract(自定义)类的实例。这是我的模型:

internal class Contract
{
    public string Name { get; set; }
    public ObservableCollection<Commission> Commissions { get; set; }
}

internal class Commission
{
    public string Id { get; set; }
}

仅供参考 - 我的模型实际上更复杂;我的类包含的成员比显示的要多,它们有构造函数,并且它们实现了 INotifyPropertyChanged。

在我的测试中,我将两个 Commission 对象加载到一个 Contract 对象中。列表框按预期工作:我可以在合同中看到每个佣金对象的 ID。 TreeView 不起作用:它在 TreeView 控件中返回一个“System.Windows.HierarchicalDataTemplate”字符串,我希望在其中列出每个委员会 ID。

我已经提到 other postsMSDN 无济于事。非常感谢您的帮助!

【问题讨论】:

  • 如果您将&lt;TextBlock Text="{Binding Id}" /&gt; 更改为&lt;TextBlock Text="{Binding Name}" /&gt; 会怎样。告诉我们您可能在控制台中看到的一些绑定错误。
  • 同样的结果;它在 TreeView 控件中返回一个“System.Windows.HierarchicalDataTemplate”字符串。我没有看到任何错误消息。

标签: c# wpf xaml treeview


【解决方案1】:

据我所知,您没有在 XAML 中正确使用 TreeView。您需要将HierarchicalDataTemplate 置于TreeView.Resources 级别并分配ItemsSource

如图here你要设置item的Template。

尝试做这样的事情:

<TreeView ItemsSource="{Binding Contracts}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type m:Contract}"
                                  ItemsSource="{Binding Commissions}">
            <TextBlock Text="{Binding Name}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type m:Commission}"
                                  ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Id}" />
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

我个人是这样做的——使用 RadTreeView:

<telerik:RadTreeView.ItemTemplate>
    <HierarchicalDataTemplate DataType="{x:Type vm:BaseType}"
                              ItemsSource="{Binding Children}">
        <TextBlock Text="{Binding Title}" />
    </HierarchicalDataTemplate>
</telerik:RadTreeView.ItemTemplate>

【讨论】:

  • 谢谢。但是,这并不能完全解决我的问题。我会发布一个后续问题。
  • 您应该指定 DataType="{x:Type m:Commission}" ItemsSource="{Binding Products}",然后您应该为 x:Type m:Product 我相信添加另一个 HierarchicalDataTemplate。
  • 嗨,NETscape。如果您愿意尝试回答我的后续问题,请点击此处:link
猜你喜欢
  • 2011-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
相关资源
最近更新 更多