TreeView 中的“SelectedItem"方法 [英] 'SelectedItem' method in TreeView

查看:26
本文介绍了TreeView 中的“SelectedItem"方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 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)
{
}

只要单击 TreeView 上的任意位置即可启动此方法.

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

突出显示的值是我希望方法在点击时触发的值.

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

推荐答案

我会在 SelectionChanged 处理程序中执行此操作,但是我无法想象它在 MouseDown 中对此会有任何不同:

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);
}

如果可能的话,我会推荐一个继承结构,这将防止需要 try/catch.因为您可以检查正在使用哪个子类.这在这里可能无法实现,但我建议在未来的项目中牢记这一点.

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.

这篇关于TreeView 中的“SelectedItem"方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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