如何在WPF树视图的同一级别中显示XML节点和子节点的值? [英] How do I display the value of an XML node and a child node in the same level of a WPF treeview?

查看:89
本文介绍了如何在WPF树视图的同一级别中显示XML节点和子节点的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个管理录音的应用程序。从理论上讲,My Series的新录音将放在\\Library \ Myy Series文件夹中,但有两个复杂情况。首先,新的录音可能会以其他名字出现在我面前;例如麦克白可能成为威廉莎士比亚的麦克白。其次,名称可以包含文件夹名称中不允许的字符 - 例如Who Framed Roger Rabbit?。所以我有一个简单的数据结构,对于每个系列,包含在UI中使用的显示名称,用于存储的foldername和一个列表(可能是空的)别名,我有LINQ来检索另一个给定的。以下是显示数据文件结构的示例:



I have an application which manages audio recordings. In theory, new recording of "My Series" go in a folder "\\Library\My Series" but there are two complications. Firstly, new recordings may come to me with alternate names; for example "Macbeth" might become "William Shakespeare's Macbeth". Secondly, names can contain characters not allowed in folder names - for example "Who Framed Roger Rabbit?". So I have a simple data structure which contains, for each series, the display name for use in the UI, the foldername for use in storage and a list (possibly empty) of aliases, and I have LINQ to retrieve one given another. Here's an example to show the structure of the data file:

<?xml version="1.0" encoding="utf-8"?>
<recordings>
  <series>
    <seriestitle>Series 1</seriestitle>
    <seriesfolder>Series 1</seriesfolder>
    <alias>Series One</alias>
    <alias>Series 1.0</alias>
  </series>
  <series>
    <seriestitle>Series 2:The Return</seriestitle>
    <seriesfolder>Series 2_The Return</seriesfolder>
    <alias>Series Two</alias>
  </series>
  <series>Example with title as text of Series node -> This is series three
    <seriestitle>Series 3 - Why?</seriestitle>
    <seriesfolder>Series 3 - Why_</seriesfolder>
    <alias>Series three alias 1</alias>
    <alias>Series 3.0</alias>
    <alias>Series 3.0 (Premier)</alias>
  </series>
</recordings>





我想在我的界面中添加所有这些数据的视图,所以我'我一直在玩WPF TreeView。我使用了HierarchicalDataTemplate,但我遇到了一个问题。我显示[NodeName] [NodeValue],它可以工作 - 但是为< series>提供整个节点内容。因为它应该。我可以压制< series>字段,它工作,但如果我折叠树以获得更多的一个屏幕,而不是系列标题列表,我反复得到字段名称 - 对导航不是很有用。这是我的主窗口的XAML和代码隐藏。





I'd like to add a view of all this data in my UI so I've been playing with a WPF TreeView. I've used a HierarchicalDataTemplate but I've run into a problem. I display [NodeName][NodeValue] which works - but gives the entire node content for <series> as it's supposed to. I can suppress the <series> field, which works, but if I collapse the tree to get more one screen, instead of a list of series titles, I get the fieldname over and over - not very useful for navigation. Here's the XAML and codebehind for my main window.

<Window x:Class="XmlDataTree.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:XmlDataTree"
        Title="XmlData Tree Test"
        Loaded="Window_Loaded">

    <Window.Resources>
        <!-- Expand all nodes on load for demo -->
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding ElementName=chkExpand, Path=IsChecked, TargetNullValue=false}"/>
        </Style>

        <HierarchicalDataTemplate x:Key="NodeTemplate">
            <StackPanel Orientation="Horizontal" Focusable="False">
                <TextBlock x:Name="tbName" Text="{Binding Name}" />
                <TextBlock Text=": " />
                <TextBox x:Name="tbValue" Text="{Binding Value}" />
            </StackPanel>
            <!-- Hide series data -->
            <HierarchicalDataTemplate.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding Path=Name.LocalName}" Value="series"/>
                        <Condition Binding="{Binding ElementName=chkHide, Path=IsChecked, TargetNullValue=false}" Value="True"/>
                    </MultiDataTrigger.Conditions>
                    <Setter TargetName="tbValue" Property="Visibility" Value="Collapsed"/>
                </MultiDataTrigger>
            </HierarchicalDataTemplate.Triggers>
            <HierarchicalDataTemplate.ItemsSource>
                <Binding Path="Elements" />
            </HierarchicalDataTemplate.ItemsSource>
        </HierarchicalDataTemplate>
    </Window.Resources>

    <Grid Margin="4">
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="Expand Tree" />
                <CheckBox Name="chkExpand" IsChecked="True" />
                <TextBlock Text="Hide Series" Margin="10,0"/>
                <CheckBox Name="chkHide" IsChecked="False" />
            </StackPanel>
            <TreeView 
            Name="trview"
            ItemsSource="{Binding Path=Root.Elements}"
            ItemTemplate="{StaticResource NodeTemplate}"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            />
        </StackPanel>
    </Grid>
</Window>










using System.Windows;
using System.Xml.Linq;

namespace XmlDataTree
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            XDocument xDoc = new XDocument();
            xDoc = XDocument.Load(System.IO.Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory) + "aliases.xml");
            trview.DataContext = xDoc;
        }
    }
}





如果你运行它,你会看到系列字段包含大量信息。如果通过勾选复选框隐藏序列字段,则可以更清楚地看到数据。或者,如果您通过勾选另一个框来折叠树,则可以看到更多系列,并且系列字段作为索引并不太可怕。但如果你两个都做。所有你得到的是一系列的系列一遍又一遍。



有没有办法< series>字段折叠/展开能够保留,但显示< seriestitle> childnode而不是< series>节点本身?我觉得我应该能够通过替换来管理某些事情。





If you run this, you'll see the "series" field contains a lot of info. If you hide the series field by ticking the checkbox you can see the data clearer. Alternately if you collapse the tree by ticking the other box, you can see many more series and a "series" field isn't too terrible as an index. But if you do both. all you get is a list of the word "series" over and over.

Is there a way for the "<series>" field collapse/expand ability to be preserved but to display the <seriestitle> childnode rather than the <series> node itself? I feel I should be able to manage something by replacing

<Setter TargetName="tbValue" Property="Text" Value="{Value}"/>



with




with

<Setter TargetName="tbValue" Property="Text" Value="{Binding Path=node::childnode[seriestitle].text()"/>





我尝试了什么:



我花了很长时间用谷歌搜索并在Path和XPath,XDocuments和XmlDocuments之间混淆还有很多。它可能没有帮助,我不是专业人士,这只是一个爱好;-)



所有建议感激不尽!



What I have tried:

I've spent a long time Googling and getting confused between Path and XPath, XDocuments and XmlDocuments and many more. It probably doesn't help than I'm not a pro, this is just a hobby ;-)

All suggestions gratefull received!

推荐答案

我编写了一个转换器,它接受当前节点并返回seriestitle子节点的文本。我还没有编写Convertback!



I've written a Converter which accepts the current node and returns the text of the seriestitle child node. I haven't written the Convertback yet though!

public class seriesTitleConverter : IValueConverter
    {
        public Object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null) return string.Empty;
            try
            {
                XElement xe = value as XElement;
                XElement ce = xe.Element("seriestitle");
                return (ce.Value);
            }
            catch (Exception ex)
            {
                return "No title node found";
            }
        }
        public Object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }


这篇关于如何在WPF树视图的同一级别中显示XML节点和子节点的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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