none
动态生成树的问题 RRS feed

  • 问题

  • 现在我有一个存储了树节点和一些信息的XML,XML如下:

      <Node>
        <NodeName>进水区</NodeName>
        <Params>
        </Params>
        <SubNodes>
          <Node>
            <NodeName>进水模式参数</NodeName>
            <Params>
              <Param>
                <ParamName>ASM2dInfluent_FlowMode</ParamName>
                <ParamDetails>流量类型</ParamDetails>
                <ParamUnits>--</ParamUnits>
                <ParamDefaultValue>1</ParamDefaultValue>
                <ParamUserValue>1</ParamUserValue>
              </Param>
              <Param>
                <ParamName>ASM2dInfluent_WaterFlowNomal</ParamName>
                <ParamDetails>正常模式</ParamDetails>
                <ParamUnits>m3/d</ParamUnits>
                <ParamDefaultValue>2000</ParamDefaultValue>
                <ParamUserValue>2000</ParamUserValue>
              </Param>
            </Params>
            <SubNodes></SubNodes>
          </Node>
          <Node>
            <NodeName>进水浓度</NodeName>
            <Params>
              <Param>
                <ParamName>ASM2dInfluent_soin</ParamName>
                <ParamDetails>溶解氧</ParamDetails>
                <ParamUnits>--</ParamUnits>
                <ParamDefaultValue>0</ParamDefaultValue>
                <ParamUserValue>0</ParamUserValue>
              </Param>
              <Param>
                <ParamName>ASM2dInfluent_xtssin</ParamName>
                <ParamDetails>惰性无机颗粒物</ParamDetails>
                <ParamUnits>--</ParamUnits>
                <ParamDefaultValue>160</ParamDefaultValue>
                <ParamUserValue>160</ParamUserValue>
              </Param>
            </Params>
            <SubNodes></SubNodes>
          </Node>
        </SubNodes>
      </Node>

    其中每个Node是TreeView的一个节点,如果subNode不是空,则要求继续创建TreeView的节点。每个节点都包含一些Param,TreeView的叶子节点Param才不是空,父节点的Param都是空,当点击叶子节点的时候,我要把Param的信息显示在一个ListView中。我不知道如何动态创建这样的一棵树,以及如何将叶子节点的Param绑定到ListView中,谁能帮我看下这个该如何设计,用到哪些相关的WPF技术。初学WPF,不胜感激~~

    2011年9月21日 2:12

答案

  • 参考 Binding.XPath: http://msdn.microsoft.com/en-us/library/system.windows.data.binding.xpath.aspx  

     How To Use XPath and an XML Data Island in a WPF Application: http://vbcity.com/blogs/xtab/archive/2010/12/22/how-to-use-xpath-and-an-xml-data-island-in-a-wpf-application.aspx

    根据的描述,我做了个例子:

    <Window x:Class="WpfApplication3.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
      <Window.Resources>
        <XmlDataProvider x:Key="XMLData" Source="Data.xml" XPath="Node"/>
        <DataTemplate DataType="Node">
          <TreeViewItem x:Name="item" Header="{Binding XPath=NodeName}"/>
            <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=Count, XPath=SubNodes/Node}" Value="0">
              <Setter TargetName="item" Property="ItemsSource" Value="{Binding XPath=Params/Param}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=Count, XPath=Params/Param}" Value="0">
              <Setter TargetName="item" Property="ItemsSource" Value="{Binding XPath=SubNodes/Node}"/>
            </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
        <DataTemplate DataType="Param">
          <StackPanel>
            <TextBlock Text="{Binding XPath=ParamName, StringFormat={}Param Name: {0}}"/>
            <TextBlock Text="{Binding XPath=ParamDetails, StringFormat={}Param Details: {0}}"/>
            <TextBlock Text="{Binding XPath=ParamUnits, StringFormat={}Param Units: {0}}"/>
            <TextBlock Text="{Binding XPath=ParamDefaultValue, StringFormat={}Param Default Value: {0}}"/>
            <TextBlock Text="{Binding XPath=ParamUserValue, StringFormat={}Param User Value: {0}}"/>
          </StackPanel>
        </DataTemplate>
      </Window.Resources>
      <Grid>
        <TreeView ItemsSource="{Binding Source={StaticResource XMLData}}"/>
      </Grid>
    </Window>
    
    


    下载: https://skydrive.live.com/?cid=51b2fdd068799d15#!/?cid=51b2fdd068799d15&sc=documents&uc=1&id=51B2FDD068799D15%21838 

     

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年9月21日 12:25
    版主