如何将Xml属性绑定到Treeview节点,同时将XDocument数据绑定到WPF Treeview [英] How to bind Xml Attribute to Treeview nodes, while databinding XDocument to WPF Treeview

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

问题描述

我有一个XML需要被数据绑定到一个 WPF TreeView 。这里XML可以有不同的结构。 TreeView应该是足够数字化的,足以加载层次结构的任何排列。然而,节点上的 XAttribute (称为标题)应该是数据绑定到TreeViewItem的标题文本而不是节点名称

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:

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

XAML的代码将XML加载到XDocument并将其绑定到TreeView

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}"/>

但是,我想将其绑定到Section,Loop的标题属性和上面的Xml中的页面。我读到,在绑定XDocument时不可能使用XPath。但是,必须有一种方法来将标题属性绑定到TreeViewItem的标题文本。我尝试使用@Title,。[@ 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论坛有类似的讨论。

任何指针都将非常有用。

Any pointers would be greatly helpful.

推荐答案

Hurray!我想出了如何绑定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}"/>

很难想象Title可以直接用在方括号中。

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

更多@此MSDN链接

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

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