树状视图项目单击事件 [英] Tree view item click event

查看:61
本文介绍了树状视图项目单击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用WPF树视图,....
我在项目中附加了树状视图,以显示图书库.树形视图绑定到一个XML文件,该文件包含类别和书名以及对书的封面图像的引用.
XML文件为:

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:

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


我想在网格中选择的树状视图项目上显示相应的图像,但是找不到找到方法,在单击鼠标左键时返回树状视图项目选择中的ref 属性.
请帮忙.


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.

推荐答案

关键是:您的树视图项目的Content可以是任何类型.

确定数据类型是否代表您的书:

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

Make if a data type representing your book:

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



此处的键已覆盖object.ToString-这是您希望在代表该书的树形视图的节点中看到的内容.您可以扩展此类以表示不同的分类器.

据我了解,您已经知道如何处理System.Windows.Controls.TreeView.SelectedItemChanged.

如果发生此事件,则应将注释内容转换为TreeViewContent类型,并在句柄代码中使用TreeViewContent.Reference属性(或任何其他属性).

—SA



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


这篇关于树状视图项目单击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆