如何将第一个项目设置为选中

时间:2021-10-16 23:01:03

I have 2 ListView controls here, let's say Listview1 and Listview2, respectively. What I would like to achieve is that I want the first item in Listview2 to be selected & highlighted whenever Listview1's SelectionChanged event is triggered.

我这里有2个ListView控件,分别是Listview1和Listview2。我想要实现的是,每当触发Listview1的SelectionChanged事件时,我都希望选择并突出显示Listview2中的第一项。

I have tried to use the following line of code to make it happened but I guess it's not correct.

我试图使用以下代码行来实现它,但我想这不正确。

private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{    
    Listview2.SelectedIndex = 0;
}

The first item in Listview2 is still not selected & highlighted. Can anyone help? Thanks very much in advance.

Listview2中的第一项仍未选中和突出显示。有人可以帮忙吗?首先十分感谢。

Edit: That line is correct. It didn't work because I placed it before the line of code that was doing dynamic loading. No wonder.... :)

编辑:那条线是正确的。它没有用,因为我把它放在进行动态加载的代码行之前。难怪.... :)

3 个解决方案

#1


0  

I tried with this code and its running fine.

我试过这个代码,运行正常。

private void listView1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        try  
        {      
            //listView.Focus();              
            listView2.Items[0].Selected = true;  
        }  
        catch { }  
    }

But after running its output when i select the first item in "listView1" the item of "listView2" is selected but you can not see it because the focus on Listview1. When you click on the listView2 then you will see a blink of selected item. Ithink there is no way to focus on two listview at the same time. When you will uncomment the "listView.Focus()" then you will see that the selected item is highlighted.

但是当我选择“listView1”中的第一项后运行其输出时,选择了“listView2”项,但由于焦点在Listview1上,您无法看到它。当您单击listView2时,您将看到所选项目的闪烁。我认为没有办法同时关注两个列表视图。当您取消注释“listView.Focus()”时,您将看到所选项目突出显示。

#2


0  

Try

private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{    
((ListViewItem)Listview2.Items[0].Selected) = true;
}

#3


0  

I cannot see any problem with your code. I think you cannot see the highlighting because ListView2 items are not focused. Make the item Focused and see.

我看不到您的代码有任何问题。我认为您无法看到突出显示,因为ListView2项目没有聚焦。使项目聚焦并查看。

#1


0  

I tried with this code and its running fine.

我试过这个代码,运行正常。

private void listView1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        try  
        {      
            //listView.Focus();              
            listView2.Items[0].Selected = true;  
        }  
        catch { }  
    }

But after running its output when i select the first item in "listView1" the item of "listView2" is selected but you can not see it because the focus on Listview1. When you click on the listView2 then you will see a blink of selected item. Ithink there is no way to focus on two listview at the same time. When you will uncomment the "listView.Focus()" then you will see that the selected item is highlighted.

但是当我选择“listView1”中的第一项后运行其输出时,选择了“listView2”项,但由于焦点在Listview1上,您无法看到它。当您单击listView2时,您将看到所选项目的闪烁。我认为没有办法同时关注两个列表视图。当您取消注释“listView.Focus()”时,您将看到所选项目突出显示。

#2


0  

Try

private void ListView1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{    
((ListViewItem)Listview2.Items[0].Selected) = true;
}

#3


0  

I cannot see any problem with your code. I think you cannot see the highlighting because ListView2 items are not focused. Make the item Focused and see.

我看不到您的代码有任何问题。我认为您无法看到突出显示,因为ListView2项目没有聚焦。使项目聚焦并查看。