简体   繁体   English

将TreeViewItem标记传递给事件处理程序c#

[英]Pass TreeViewItem Tag to Event Handler c#

Ok, Below is my code so far i am attempting to add a menu to a Custom File Browser. 好的,到目前为止,这是我到目前为止的代码,我正在尝试向自定义文件浏览器添加菜单。 Right now I am working on a new folder button but want to add more items to the menu eventually. 目前,我正在使用新的文件夹按钮,但最终希望将更多项目添加到菜单中。 My question is how to fist pass the tag from the item to the Menu_MouseLeftClick Event Handler. 我的问题是如何首先将标签从项目传递到Menu_MouseLeftClick事件处理程序。 Thanks in advance for help I am new thanks in advance. 在此先感谢您的帮助,在此先感谢我。

using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Collections.Generic;
using System;

namespace TreeViewWithMenu
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        //Create Objects and Handlers
        InitializeComponent();
        PopTree();
        SetMenu();
        this.treeView.MouseRightButtonDown += TreeView_MouseRightButtonDown;
        this.menu.MouseLeftButtonDown += Menu_MouseLeftButtonDown;

    }

    private void TreeView_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    { 
        //Make Folder Visable
        this.menu.Visibility = Visibility.Visible;
    }

    private void SetMenu()
    {
        //Set Menu for new Folder
        MenuItem MeItem = new MenuItem();
        MeItem.Header = "New Folder";
        MeItem.Tag = "New Folder";
        menu.Items.Add(MeItem);
        menu.Visibility = Visibility.Hidden;

    }

    private void Menu_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        //Create New FOlder
        Directory.CreateDirectory("");
    }



    internal void PopTree()
    {
        //Populate Tree
        List<string> CleanDirs = new List<string>();

        string [] Dirs =  Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));

        string temp;
        foreach (string dir in Dirs)
        { temp = SplitPath(dir); CleanDirs.Add(temp.ToUpper()); }

        foreach(var ShowIn in CleanDirs)
        {
            TreeViewItem TreeViewDirectory = new TreeViewItem();
            TreeViewDirectory.Tag = ShowIn;
            TreeViewDirectory.Header = ShowIn;
            TreeViewDirectory.Focusable = true;
            this.treeView.Items.Add(TreeViewDirectory);

        }

    }

    private string SplitPath(string path)
    {
        string[] temp = path.Split('\\');

        return temp[temp.Length - 1];
    }
}

} ` }`

Use the SelectedItem property to access the currently selected item from the TreeView , which should be the same item you right-clicked on. 使用SelectedItem属性从TreeView访问当前选择的项目,该项目应与您右键单击的项目相同。

private void TreeView_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
    if (treeView.SelectedItem == null)
        return;

    var selectedTag = ((TreeViewItem)treeView.SelectedItem).Tag;

    // do something with the tag
}

Don't add control (TreeViewItem) manually. 不要手动添加控件(TreeViewItem)。 Use Collection as view model Bind to the collection from the treeView ItemsSource property. 将集合用作视图模型从treeView ItemsSource属性绑定到集合。 Use DataTemplate to bind to the relavent properties on the view model to the dependency properties on the TreeViewItem control. 使用DataTemplate将视图模型上的relavent属性绑定到TreeViewItem控件上的依赖项属性。

All the pluming should be from the Xaml file 所有的内容都应该来自Xaml文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM