WPF - 我被 TreeView 控件难住了 [英] WPF - I'm stumped on the TreeView control

查看:37
本文介绍了WPF - 我被 TreeView 控件难住了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

双关语.

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

I want to create a simple TreeView using the HierarchicalDataTemplate class.

这是我的 XAML 问题:

Here's my problem 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 模式.静态资源myDataProvider"返回合同(自定义)类的实例.这是我的模型:

I'm using the MVVM pattern. The StaticResource "myDataProvider" returns an instance of a Contract (custom) class. Here's my model:

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

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

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

FYI - my model is actually more complex; my classes contain more members than shown, they have constructors, and they implement INotifyPropertyChanged.

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

In my test, I load two Commission objects into a Contract object. The listbox works as expected: I can see the Id of each Commission object w/in Contract. The TreeView doesn't work: it returns a "System.Windows.HierarchicalDataTemplate" string in the TreeView control where I'd expect each Commission Id to be listed.

我参考了其他帖子MSDN 无济于事.我会很感激你的帮助!

I've referred to other posts and MSDN to no avail. I'd be appreciative of your help!

推荐答案

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

From what I can tell is that you're not using the TreeView correctly in XAML. You need to put your HierarchicalDataTemplate at the TreeView.Resources level and assign a ItemsSource

如图所示此处 您要设置项目的模板.

As shown here you want to set the Template of the item.

尝试做这样的事情:

<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:

I personally do it like this--using RadTreeView:

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

这篇关于WPF - 我被 TreeView 控件难住了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆