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

将TreeViewItem标记传递给事件处理程序c#,c#,wpf,treeview,C#,Wpf,Treeview,好的,下面是我的代码,到目前为止,我正在尝试向自定义文件浏览器添加菜单。现在我正在开发一个新的文件夹按钮,但最终我想在菜单中添加更多的项目。我的问题是如何首先将标记从项传递到菜单\u MouseLeftClick事件处理程序。提前谢谢你的帮助我是新来的提前谢谢 using System.Windows; using System.Windows.Controls; using System.IO; using System.Collections.Generic; using System;

好的,下面是我的代码,到目前为止,我正在尝试向自定义文件浏览器添加菜单。现在我正在开发一个新的文件夹按钮,但最终我想在菜单中添加更多的项目。我的问题是如何首先将标记从项传递到菜单\u MouseLeftClick事件处理程序。提前谢谢你的帮助我是新来的提前谢谢

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];
    }
}
使用System.Windows;
使用System.Windows.Controls;
使用System.IO;
使用System.Collections.Generic;
使用制度;
命名空间树视图WithMenu
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
//创建对象和处理程序
初始化组件();
PopTree();
SetMenu();
this.treeView.MouseRightButtonDown+=treeView\u MouseRightButtonDown;
this.menu.MouseLeftButtonDown+=menu\u MouseLeftButtonDown;
}
私有void树查看_MouseRightButtonDown(对象发送器,System.Windows.Input.MouseButtonEventArgs e)
{ 
//使文件夹可见
this.menu.Visibility=可见性.Visibility;
}
私有void SetMenu()
{
//设置新文件夹的菜单
MenuItem MeItem=新MenuItem();
MeItem.Header=“新建文件夹”;
MeItem.Tag=“新建文件夹”;
菜单项添加(MeItem);
menu.Visibility=Visibility.Hidden;
}
私有无效菜单\u MouseLeftButtonDown(对象发送器,System.Windows.Input.MouseButtonEventArgs e)
{
//创建新文件夹
Directory.CreateDirectory(“”);
}
内部void PopTree()
{
//填充树
List CleanDirs=新列表();
字符串[]Dirs=Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Desktop));
字符串温度;
foreach(Dirs中的字符串dir)
{temp=SplitPath(dir);CleanDirs.Add(temp.ToUpper());}
foreach(在CleanDirs中显示的变量)
{
TreeView项目TreeView目录=新建TreeView项目();
TreeViewDirectory.Tag=ShowIn;
TreeViewDirectory.Header=ShowIn;
TreeViewDirectory.Focusable=true;
this.treeView.Items.Add(TreeViewDirectory);
}
}
专用字符串拆分路径(字符串路径)
{
字符串[]temp=path.Split('\\');
返回温度[温度长度-1];
}
}
}
`

使用
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
}
不要手动添加控件(TreeViewItem)。 将集合用作视图模型 从treeView ItemsSource属性绑定到集合。 使用DataTemplate将视图模型上的relavent属性绑定到TreeViewItem控件上的依赖项属性

所有羽化都应该来自Xaml文件