简体   繁体   中英

'SelectedItem' method in TreeView

I have a TreeView, which starts at 'To Do List' header, when you Expand that there is 'Audit' and 'Error' and then finally you Expand them their would be all different kinds of things that needs Auditing.

How do create a method for when someone clicks on something inside 'Audit'

private void MyTreeView_MouseDown(object sender, MouseButtonEventArgs e)
{
}

This method starts when you just click anywhere on the TreeView.

I want it so when I Expand 'To Do List' and also Expand 'Audit' or 'Error' the method doesn't start. I only want the method to activate when they click something inside 'Audit'.

EDIT:

在此处输入图片说明

The highlighted value is the one I want the method to trigger when clicked.

I would do this in the SelectionChanged handler, however I can't imagine it being in MouseDown would be any different for this:

// initialise entityTask here if needed outside of the scope of the try/catch
try
{
    cAuditTaskEntity entityTask = (cAuditTaskEntity)tvTasks.SelectedItem;

    ... your logic here ...
}
catch(InvalidCastException invEx)
{
    // do nothing here, another type of task has been selected, cErrorTaskEntity for example
}
catch(Exception ex)
{
    throw new Exception("something else went wrong!", ex);
}

If possible I would recommend an inheritance structure, this would prevent the need for a try/catch. As you could do a check on which child class is in use. This might not be possible here, but I'd recommend keeping it in mind for future projects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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