5
votes

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.

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.

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
Are you dynamically loading the contents of the second view? If so, you need to hold on changing the index until after it is loaded. Right now, you just have a race condition and you're losing.Jeff Mercado
The code you posted should work. Did you set a breakpoint to verify the posted code is being executed? What else is going on - e.g. databinding, updating the items, anything like that?default.kramer
@JefMercado: This sounds like the only reasonable answer to me, how about posting it as such?H.B.
Thanks Jeff. You just inspired me :D I got it working now.woodykiddy
@Jeff Mercado: Told ya! (I know you didn't get much of a chance to react yet :P)H.B.

3 Answers

0
votes

Try

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

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.

0
votes

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.