Click here to Skip to main content
15,031,680 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I want to show image files in grid. which are to be selected from the tree view as names and the tree view is binded with a xml file, i am showing the names of images and i want to use the ref attribute which i had defined in my xml which is path of the image.
Please any one understand my problem and give me a solution to this.

the xml is:
XML
<Library>
    <Category CategoryName="All" Name="All">
            <Book CategoryName="Story" Name="The Monk" ref=C:\Users\Public\Pictures\Sample Pictures\page2/>
        <Book CategoryName="Story" Name="Photos" ref=C:\Users\Public\Pictures\Sample Pictures\page3/>
            <Book CategoryName="Horror" Name="Better You" ref=C:\Users\Public\Pictures\Sample Pictures\page4/>
        <Book CategoryName="Horror" Name="Dracula" ref=C:\Users\Public\Pictures\Sample Pictures\page5/>
    </Category>

</Library>


and i am using the tree view as:
XML
<Window.Resources>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Elements}" x:Key="Library">
            <TreeViewItem Header="{Binding Path=Attribute[Name].Value}"/>
        </HierarchicalDataTemplate>
        <!--<XmlDataProvider x:Key="XMLdata" Source="C:\Users\SBS\Desktop\wizaed.xml" ></XmlDataProvider>-->
    </Window.Resources>


CSS
<TreeView MouseUp="_treeView_MouseUp" Cursor="Hand" x:Name="_treeView" Width="160" Background="Transparent" Margin="0,10,10,10" BorderBrush="Transparent" BorderThickness="0"
                              ItemsSource="{Binding Path=Root.Elements}"
                              ItemTemplate="{StaticResource ResourceKey=Library}" />



and the code behind is:

XDocument XDoc = XDocument.Parse(File.ReadAllText(@"C:\Users\Public\Mylibrary.xml"));
_treeView.DataContext = XDoc;
Posted

1 solution

Are you trying to achieve something like this?
XML
<Window.Resources>
   <XmlDataProvider x:Key="XMLdata" Source="C:\DirectoryStructure\Library.xml" />
   <HierarchicalDataTemplate x:Key="Library" ItemsSource="{Binding XPath=Elements}">
      <TreeViewItem>
         <TreeViewItem.Header>
           <StackPanel Orientation="Horizontal">
             <Image Source="{Binding XPath=@ref}" />
             <TextBlock Text="{Binding XPath=@Name}" />
           </StackPanel>
         </TreeViewItem.Header>
      </TreeViewItem>
   </HierarchicalDataTemplate>
</Window.Resources>
<Grid>
<TreeView Cursor="Hand" x:Name="_treeView" Width="160" Background="Transparent" Margin="0,10,10,10" BorderBrush="Transparent" BorderThickness="0"
                           ItemsSource="{Binding XPath=//Book, Source={StaticResource XMLdata}}"
                           ItemTemplate="{StaticResource ResourceKey=Library}" />
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900