简体   繁体   English

在TreeView和ListView中始终选择/突出显示一个项目C#WinForm

[英]always one item is selected/highlighted in treeview and listview c# winform

In MDI Child form, 在MDI子窗体中,

How can I force it so that one item is always selected/highlighted. 我该如何强制它以便始终选中/突出显示一项。

When I load the form, I want to see that the first node of the treeview is highlighted, even if I navigate away ie use other child forms, should always one item is highlighted. 当我加载表单时,我希望看到树视图的第一个节点是突出显示的,即使我导航离开(即使用其他子表单),也应该始终突出显示一个项目。

Also, same approach I need it to be done on a listview 另外,我需要在列表视图上执行的相同方法

Thank you 谢谢

While setting HideSelection property is necessary to highlight selected items when control is not focused, it is not enough. 当未将控件聚焦时,需要设置HideSelection属性以突出显示选定的项目,但这还不够。 Situations when item (node) is focused but not selected are perfectly valid and can be achieved by user (and item that focused but not selected is marked only when control is active). 聚焦(但未选中)项(节点)的情况是完全有效的,并且可以由用户实现(并且聚焦但未选中的项仅在控件处于活动状态时被标记)。

However you can use a simple trick to force focused item be always selected (and thus visible even when control is not active): 但是,您可以使用一个简单的技巧来强制始终选择焦点项目(即使在控件未激活的情况下也可以看到):

private void listView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
    if (e.Item.Focused)
        e.Item.Selected = true;
}

Also do not forget to focus first item (node) of your control after populating it, so one item (node) will be always highlighted. 另外,不要忘记将控件的第一个项目(节点)放在焦点之后,因此一个项目(节点)将始终突出显示。

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

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