使用XML文件-XBAP应用程序进行动态Treeview绑定

时间:2011-02-07 20:40:55

标签: wpf xml binding treeview xbap

我有一个XML文件,它最多有三个级别的子元素,所以我需要在WPF中为TreeView将文件与XBAP App动态绑定。 我们是否需要使用Hierarchical模板,或者是否有其他方法可以轻松绑定..

例如:

<TriggerList>
  <Trigger>
    <TriggerName>trig1</TriggerName>
    <TriggerSource>manual</TriggerSource>
    <TriggerInfo>
      <Alaramid>2312</Alaramid>
      <Area>area1</Area>
      <ToolType>OLF121</ToolType>
    </TriggerInfo>
  </Trigger>
</TriggerList>

我想通过动态绑定在树视图中进行此操作..

答案是:

<UserControl x:Class="XTREAMSUI.UserControls.ExecutionList"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:mvvm="clr-namespace:MvvmFoundation.Wpf;assembly=MvvmFoundation.Wpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="230">

    <UserControl.Resources>

        <HierarchicalDataTemplate  ItemsSource="{Binding RFCNameList}" x:Key="RFCTemplate">
            <TextBlock Text="{Binding RFCName}" />
        </HierarchicalDataTemplate>


        <HierarchicalDataTemplate  ItemsSource="{Binding RFCNameList}" x:Key="TriggerIDTemplate"  ItemTemplate="{StaticResource RFCTemplate}">
            <TextBlock Text="{Binding TriggerID}" IsEnabled="False"/>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate  ItemsSource="{Binding TriggerIDList}" x:Key="TriggerSourceTemplate" ItemTemplate="{StaticResource TriggerIDTemplate}">
            <TextBlock Text="{Binding TriggerSource}" IsEnabled="False"/>
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate ItemsSource="{Binding TriggerList}" x:Key="TriggerTemplate" ItemTemplate="{StaticResource TriggerSourceTemplate}">
            <TextBlock Text="{Binding HostMachineName}" IsEnabled="False"/>
        </HierarchicalDataTemplate>

        <Style TargetType="TreeViewItem">
            <Setter Property="IsExpanded" Value="True" />
        </Style>


    </UserControl.Resources>

    <Grid Name="gridExecutionlst" IsEnabled="{Binding IsExecutionListEnabled, Mode=TwoWay}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <!--<TreeView Grid.Row="0" Name="treeviewExecutionList" Height="400" Width="200" ItemsSource="{Binding TriggerListXML}" ItemTemplate="{StaticResource dataTemplateExecutionList}"></TreeView>-->

        <TreeView Name="treeviewExecutionList" Height="450" Width="230" FontWeight="Normal" ItemsSource="{Binding Path=TriggerList,Mode=TwoWay}" ItemTemplate="{StaticResource TriggerTemplate}">

            <mvvm:CommandBehaviorCollection.Behaviors>
                <mvvm:BehaviorBinding Command="{Binding ListBoxCommand}" Event="MouseDoubleClick" CommandParameter="{Binding ElementName=treeviewExecutionList, Path=SelectedItem}"/>
            </mvvm:CommandBehaviorCollection.Behaviors>

        </TreeView>
    </Grid>
</UserControl>

1 个答案:

答案 0 :(得分:0)

答案是:

`

<HierarchicalDataTemplate  ItemsSource="{Binding RFCNameList}" x:Key="RFCTemplate">
    <TextBlock Text="{Binding RFCName}" />
</HierarchicalDataTemplate>


<HierarchicalDataTemplate  ItemsSource="{Binding RFCNameList}" x:Key="TriggerIDTemplate"  ItemTemplate="{StaticResource RFCTemplate}">
    <TextBlock Text="{Binding TriggerID}" IsEnabled="False"/>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate  ItemsSource="{Binding TriggerIDList}" x:Key="TriggerSourceTemplate" ItemTemplate="{StaticResource TriggerIDTemplate}">
    <TextBlock Text="{Binding TriggerSource}" IsEnabled="False"/>
</HierarchicalDataTemplate>

<HierarchicalDataTemplate ItemsSource="{Binding TriggerList}" x:Key="TriggerTemplate" ItemTemplate="{StaticResource TriggerSourceTemplate}">
    <TextBlock Text="{Binding HostMachineName}" IsEnabled="False"/>
</HierarchicalDataTemplate>

<Style TargetType="TreeViewItem">
    <Setter Property="IsExpanded" Value="True" />
</Style>

                               

<TreeView Name="treeviewExecutionList" Height="450" Width="230" FontWeight="Normal" ItemsSource="{Binding Path=TriggerList,Mode=TwoWay}" ItemTemplate="{StaticResource TriggerTemplate}">

    <mvvm:CommandBehaviorCollection.Behaviors>
        <mvvm:BehaviorBinding Command="{Binding ListBoxCommand}" Event="MouseDoubleClick" CommandParameter="{Binding ElementName=treeviewExecutionList, Path=SelectedItem}"/>
    </mvvm:CommandBehaviorCollection.Behaviors>

</TreeView>