开发者

TreeViewItem MouseDoubleClick not proving correct sender in event handler

I have a treeview with some nodes along with some subnodes.In those subnodes, I have attached an event handler for MouseDoubleClick.

But in the event handler, the sender parameter is referring to the the parent node, not the subnode where I am double clicking.

private void Window_Initialized(object sender, EventArgs e)
{

    string[] drives = System.Environment.GetLogicalDrives();
    foreach (string drive in drives)
    {
        TreeViewItem node = new TreeViewItem(){Header=drive,Tag=drive};
        TreeViewFolders.Items.Add(node);
        node.MouseDoubleClick+=new MouseButtonEventHandler(rootUI_MouseDoubleClick);
    }
}

void rootUI_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    TreeViewItem node = sender as TreeViewItem;
    node.Items.Clear();
    string folder = ((TreeViewFolders.SelectedIte开发者_JAVA技巧m as TreeViewItem).Tag) as string;

        try
        {
            DirectoryInfo[] subDirectories = new DirectoryInfo(folder).GetDirectories();
            foreach (DirectoryInfo sd in subDirectories)
            {
                TreeViewItem subnode = new TreeViewItem() { Header = sd.Name,Tag=sd.FullName};
                node.MouseDoubleClick += new MouseButtonEventHandler(rootUI_MouseDoubleClick);
                node.Items.Add(subnode);
            }
        }
        catch
        {
        }
        e.Handled = true;
}

At the topmost level, there are logical drives and then subfolders and so on.

Whesn I click the subnode(the folders in logical drives), the sender parameter is still the local drives, not the particular subnode.

It works OK when I use SelectedItem instead of sender.


Your code adds another double-click handler to the same node that the double-click handler fired on. Perhaps you meant to add a handler for a double-click on the sub-node?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜