如何绑定XML属性为树视图节点,而数据绑定的XDocument到WPF树视图 [英] How to bind Xml Attribute to Treeview nodes, while databinding XDocument to WPF Treeview

查看:174
本文介绍了如何绑定XML属性为树视图节点,而数据绑定的XDocument到WPF树视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有需要的XML进行数据绑定到一个 WPF的TreeView 。这里将XML可以有不同的结构。在TreeView应该足够数据绑定通用加载层次结构的任何排列。然而,一个 XAttribute 节点上(所谓的标题)应数据绑定到树型视图的标题文字不是节点名

I have an XML that needs to be databound to a WPF TreeView. Here the XML can have different structure. The TreeView should be databound generic enough to load any permutation of hierarchy. However an XAttribute on the nodes (called Title) should be databound to the TreeViewItem's header text and not the nodename.

要绑定XML:

<Wizard>
  <Section Title="Home">
    <Loop Title="Income Loop">
      <Page Title="Employer Income"/>
      <Page Title="Parttime Job Income"/>
      <Page Title="Self employment Income"/>
    </Loop>
  </Section>
  <Section Title="Deductions">
    <Loop Title="Deductions Loop">
      <Page Title="Travel spending"/>
      <Page Title="Charity spending"/>
      <Page Title="Dependents"/>
    </Loop>
  </Section>
</Wizard>



XAML:

XAML:

<Window x:Class="Wpf.DataBinding.TreeViewer"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:Wpf.DataBinding"
    Title="TreeViewer" Height="300" Width="300">
    <Window.Resources>
    	<HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="TVTemplate">
    		<TreeViewItem Header="{Binding Path=Name}"/>
    	</HierarchicalDataTemplate>
    </Window.Resources>
    <StackPanel>
    	<TreeView x:Name="_treeView" Style="{StaticResource TVallExpanded}"
    			ItemsSource="{Binding Path=Root.Elements}"
    			ItemTemplate="{StaticResource TVTemplate}" />
    </StackPanel>
</Window>



加载XML来的XDocument并将其绑定到TreeView控件XAML的代码隐藏

XAML's codebehind that loads XML to XDocument and binds it to TreeView

public partial class TreeViewer : Window
{
    public TreeViewer()
    {
        InitializeComponent();
        XDocument doc = XDocument.Parse(File.ReadAllText(@"C:\MyWizard.xml"));
        _treeView.DataContext = doc;
    }
}



所以在XAML标记我们绑定名称到TreeViewItem的。头文件

So in the XAML markup we are binding Name to TreeViewItem's header.

<TreeViewItem Header="{Binding Path=Name}"/>



不过,我想将它绑定为标题部分,循环的属性和页面在上面的XML。我读了这是不可能的使用XPath,而绑定的XDocument。但必须有到标题属性TreeViewItem的标题文字结合的方式。我试着用@Title。[@标题]等,但似乎没有工作。

However, I want to bind it to Title attribute of Section, Loop and Page in the Xml above. I read that it's not possible to use XPath while binding XDocument. But there has to be a way to bind the Title attribute to TreeViewItem's Header text. I tried using @Title, .[@Title] etc. But none seemed to work.

这的在MSDN论坛也有类似的讨论帖。

This thread on MSDN Forums has a similar discussion.

任何指针将是很大的帮助。

Any pointers would be greatly helpful.

推荐答案

好哇!我想出如何绑定XAttribute。它不是直观,它不容易想象。但这里是如何可以做到

Hurrah !!! I figured out how to bind XAttribute. It is not intuitive and it's not easily imaginable. But here is how it can be done.

<TreeViewItem Header="{Binding Path=Attribute[Title].Value}"/>



这是很难想象标题可以直接在方括号中使用。

It is hard to imagine that Title can directly be used in square braces.

更多@此MSDN链接

这篇关于如何绑定XML属性为树视图节点,而数据绑定的XDocument到WPF树视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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