So obviously I am doing something wrong, but I just cannot seem to get the HierarchicalDataTemplate (or even just DataTemplate) to work when using the DataType property. I have created the shortest possible WPF application to demonstrate the problem.
显然,我做错了一些事情,但是在使用DataType属性时,我似乎无法让hierarchy aldatatemplate(甚至只是DataTemplate)工作。我创建了尽可能短的WPF应用程序来演示这个问题。
XAML:
XAML:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<Window.Resources>
<HierarchicalDataTemplate DataType="x:Type local:Foo">
<TextBlock Text="I am a Foo" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="x:Type System:String">
<TextBlock Text="I am a String" />
</HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Name="treeView1" ItemsSource="{Binding}" />
</Grid>
</Window>
CODE:
代码:
namespace WpfApplication1
{
public class Foo
{
public string Name { get; set; }
}
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
var list = new List<object> { "a", 1, "b", 2, new Foo() { Name="Brian"}};
treeView1.DataContext = list;
}
}
}
Obviously I am expecting it display the following in the treeview.
显然,我希望它在treeview中显示以下内容。
I am a string
1
I am a string
2
I am a foo
But my application actually displays the following.
但是我的应用程序实际上显示了以下内容。
a
1
b
2
WpfApplication1.Foo
The strange thing is that almost every example I see on the web does this very thing (with slight variations) and no one else seems to be having a problem with it. Yet I have tried countless different ways of rearranging the XAML and nothing seems to help. I am hoping another pair eyes can help.
奇怪的是,我在网上看到的几乎每一个例子都在做这件事(只是略有变化),而且似乎没有人对它有异议。然而,我尝试过无数种重新排列XAML的方法,但似乎没有任何帮助。我希望另一双眼睛能帮上忙。
3 个解决方案
#1
39
Your code is fine, but your DataType attribute values need to be wrapped in curly braces:
您的代码很好,但是您的数据类型属性值需要用大括号括起来:
<HierarchicalDataTemplate DataType="{x:Type local:Foo}">
<TextBlock Text="I am a Foo" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type System:String}">
<TextBlock Text="I am a String" />
</HierarchicalDataTemplate>
#2
3
Also if you aren't using the ItemsSource of the HierarchicalDataTemplate you may as well use a DataTemplate instead.
此外,如果您不使用层次数据呕血板的ItemsSource,您也可以使用一个数据呕血板。
#3
2
You haven't specified the ItemTemplate property of the TreeView. This property tells the TreeView which DataTemplate to use, in your case you want to specify the one defined in your resources:
您还没有指定TreeView的ItemTemplate属性。这个属性告诉TreeView使用哪个DataTemplate,在您的情况下,您希望指定资源中定义的那个:
<TreeView Name="treeView1"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource MyResourceItemTemplate}" />
But in your case you may actually want to use a DataTemplateSelector implementation applied to the TreeView.ItemTemplateSelector
property for choosing a different template based on the type to be displayed...
但是在您的示例中,您可能实际上希望使用应用于TreeView的DataTemplateSelector实现。ItemTemplateSelector属性用于根据要显示的类型选择不同的模板…
#1
39
Your code is fine, but your DataType attribute values need to be wrapped in curly braces:
您的代码很好,但是您的数据类型属性值需要用大括号括起来:
<HierarchicalDataTemplate DataType="{x:Type local:Foo}">
<TextBlock Text="I am a Foo" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type System:String}">
<TextBlock Text="I am a String" />
</HierarchicalDataTemplate>
#2
3
Also if you aren't using the ItemsSource of the HierarchicalDataTemplate you may as well use a DataTemplate instead.
此外,如果您不使用层次数据呕血板的ItemsSource,您也可以使用一个数据呕血板。
#3
2
You haven't specified the ItemTemplate property of the TreeView. This property tells the TreeView which DataTemplate to use, in your case you want to specify the one defined in your resources:
您还没有指定TreeView的ItemTemplate属性。这个属性告诉TreeView使用哪个DataTemplate,在您的情况下,您希望指定资源中定义的那个:
<TreeView Name="treeView1"
ItemsSource="{Binding}"
ItemTemplate="{StaticResource MyResourceItemTemplate}" />
But in your case you may actually want to use a DataTemplateSelector implementation applied to the TreeView.ItemTemplateSelector
property for choosing a different template based on the type to be displayed...
但是在您的示例中,您可能实际上希望使用应用于TreeView的DataTemplateSelector实现。ItemTemplateSelector属性用于根据要显示的类型选择不同的模板…