Dynamic Treeview Binding with XML file-XBAP Application

977 views Asked by At

I have a XML file which has maximum three levels of child elements so i need to bind the file with TreeView in WPF for XBAP App Dynamically. Do we need to use Hierarchical template or is there any other way to get easy binding..

for example:

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

I want to make this in tree view through Dynamic Binding ..

Answer is :

<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

There are 1 answers

0
nallskarthi On BEST ANSWER

Answer is :

`

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