Click here to Skip to main content
15,040,212 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I had just started working on WPF tree view and....
I had attached a tree view in my project to show a book library. The tree view is binded to a XML file which contain Category and book name and reference to the Cover page image of book.
XML File is:
XML
<Library Name="My Library">
    <Category CategoryName="All" Name="All">
        <Book CategoryName="Fiction" Name="The DaVinci Code" ref="C:\Users\Public\Pictures\Sample Pictures\Page_1"/>
            <Book CategoryName="Story" Name="The Monk" ref="C:\Users\Public\Pictures\Sample Pictures\Page_2"/>
        <Book CategoryName="Story" Name="Photos" ref="C:\Users\Public\Pictures\Sample Pictures\Page_3"/>
            <Book CategoryName="Horror" Name="Better You" ref="C:\Users\Public\Pictures\Sample Pictures\Page_4"/>
        <Book CategoryName="Horror" Name="Dracula"/>
    </Category>

I want to show the corresponding image on tree view item selected in grids but cant find a way to return the ref attribute on tree view item selection on left mouse click.
please help.
Posted
Updated 11-May-11 20:40pm
v2

1 solution

The key is: your tree view item's Content can be of any type.

Make if a data type representing your book:

C#
class TreeViewContent {
    internal TreeViewContent(string categoryName, string name, Uri reference) { /*...*/ }
    internal Uri Reference { get { return fReference; } }
    internal string CategoryName { get { return fCategoryName; } }
    internal string Name { get { return fName; } }
    //...
    public override string ToString() { /* calculate and return what you want to see in the tree */ }
    //...
    string fCategoryName, fName;
    Uri fReference;
} //class TreeViewContent


The key here is overridden object.ToString — this is what you want to see in the nodes of tree view representing the book. You can extend this class to represent different classifiers.

As I understand, you already know how to handle System.Windows.Controls.TreeView.SelectedItemChanged.

If this event you should cast note content to TreeViewContent type and use your TreeViewContent.Reference property (or any other properties) in your handle code.

—SA
   
v2

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