WPF TreeView HierarchicalDataTemplate 无限节点
当 xml 使用相同的类名具有无限级别时,我在填充树视图超出第一级别时遇到问题。我使用 Xsd2Code 创建对象类。
为了避免这篇文章的长度超过 3000 行,我添加了一个下载该项目的链接。可以在此处下载
我的 XML 示例
<Testing>
<Numbers>
<Number val="1">
<Number val="1.1">
<Number val="1.1.1">
<Number val="1.1.2" />
<Number val="1.1.3" />
<Number val="1.1.4" />
</Number>
</Number>
<Number val="1.2" />
<Number val="1.3" />
<Number val="1.4" />
</Number>
<Number val="2" />
<Number val="3" />
<Number val="4" />
</Numbers>
<Numbers>
<Number val="5" />
<Number val="6" />
<Number val="7" />
<Number val="8" />
</Numbers>
</Testing>
XAML
<Window.Resources>
<local:TestingXmlData x:Key="TestXML" />
</Window.Resources>
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate x:Key="MainData" ItemsSource="{Binding Number1}"
>
<Border>
<Label VerticalContentAlignment="Center">
<TextBlock Text="{Binding val}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
</Label>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="Test" ItemsSource="{Binding Number}"
ItemTemplate="{StaticResource MainData}"
DataType="{x:Type local:Number}"
>
<Border>
<Label VerticalContentAlignment="Center">
<TextBlock Text="{Binding val}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
</Label>
</Border>
</HierarchicalDataTemplate>
</Grid.Resources>
<telerik:RadTreeView ItemsSource="{Binding Source={StaticResource TestXML}, Path=TestingDataSource}"
ItemTemplate="{StaticResource Test}"
/>
</Grid>
代码隐藏
public class TestingXmlData
{
private Testing testing;
public TestingXmlData()
{
this.TestingDataSource = new ObservableCollection<Number>();
var t = AppDomain.CurrentDomain.BaseDirectory;
testing = Testing.LoadFromFile(t + @"../../Test.xml");
var numberBranch = (from tt in testing.Items
select tt.Number).ToList();
foreach (var num in numberBranch)
foreach (var entry in num)
this.TestingDataSource.Add(entry);
}
public ObservableCollection<Number> TestingDataSource
{
get;
set;
}
}
I am having problems populating my treeview beyond the first level when the xml is using the same class name to have unlimited levels. I have used Xsd2Code to create the object class.
To keep this post from being 3000 lines long, I am including a link for downloading the project. It can be downloaded here
My XML example
<Testing>
<Numbers>
<Number val="1">
<Number val="1.1">
<Number val="1.1.1">
<Number val="1.1.2" />
<Number val="1.1.3" />
<Number val="1.1.4" />
</Number>
</Number>
<Number val="1.2" />
<Number val="1.3" />
<Number val="1.4" />
</Number>
<Number val="2" />
<Number val="3" />
<Number val="4" />
</Numbers>
<Numbers>
<Number val="5" />
<Number val="6" />
<Number val="7" />
<Number val="8" />
</Numbers>
</Testing>
XAML
<Window.Resources>
<local:TestingXmlData x:Key="TestXML" />
</Window.Resources>
<Grid>
<Grid.Resources>
<HierarchicalDataTemplate x:Key="MainData" ItemsSource="{Binding Number1}"
>
<Border>
<Label VerticalContentAlignment="Center">
<TextBlock Text="{Binding val}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
</Label>
</Border>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate x:Key="Test" ItemsSource="{Binding Number}"
ItemTemplate="{StaticResource MainData}"
DataType="{x:Type local:Number}"
>
<Border>
<Label VerticalContentAlignment="Center">
<TextBlock Text="{Binding val}" VerticalAlignment="Center" TextWrapping="Wrap"></TextBlock>
</Label>
</Border>
</HierarchicalDataTemplate>
</Grid.Resources>
<telerik:RadTreeView ItemsSource="{Binding Source={StaticResource TestXML}, Path=TestingDataSource}"
ItemTemplate="{StaticResource Test}"
/>
</Grid>
Code Behind
public class TestingXmlData
{
private Testing testing;
public TestingXmlData()
{
this.TestingDataSource = new ObservableCollection<Number>();
var t = AppDomain.CurrentDomain.BaseDirectory;
testing = Testing.LoadFromFile(t + @"../../Test.xml");
var numberBranch = (from tt in testing.Items
select tt.Number).ToList();
foreach (var num in numberBranch)
foreach (var entry in num)
this.TestingDataSource.Add(entry);
}
public ObservableCollection<Number> TestingDataSource
{
get;
set;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您已经解决了这个问题,但对我来说,如果我同时定义了 x:Key 和 DataType,DataTemplates(以及 HierarchicalDataTemplate)永远无法正常工作。 此问题询问有关 DataTemplates 的数据类型:
我最终使用 x:Key 创建了一个 DataTemplate,并使用 DataType 创建了第二个 DataTemplate,其中只有一个 ContentControl,将 ContentTemplate 设置为使用 x:Key 的 DataTemplate。
Probably you already have solved this, but for me, DataTemplates (HierarchicalDataTemplate as well) never worked properly, if I had defined x:Key and DataType at the same time. This question asks about DataTemplates' DataType:
I ended up creating one DataTemplate with x:Key and second one with DataType, which had just one ContentControl that had ContentTemplate set to DataTemplate with x:Key.