问题
When the program runs, there is a series of ListView forms. We populated one of them with items (as strings) and we check whether the state of selection has changed. Once it's changed, we grab the text of the selected item using FocusedItem.Text. The first time works just fine but when another selection is made, the selected item returns as null.
The only way we can temporarily get around this issue is to clear and repopulate the form. The disadvantage is that we lose the highlighted item. There got to be another way around this. Maybe we're not clear on how ListView really works?
Any ideas?
回答1:
Put the following condition around in the OnSelectedIndexHandler
:
if(listViewObject.SelectedItems!=null&& listViewObject.SelectedItems.Count>0)
{
//....your code here
}
回答2:
If you're after the item the user has selected, you should be using the first item in the SelectedItems
collection. FocusedItem
is the item that currently has the keyboard focus (and hence is showing the dotted focus rectangle); if the control doesn't have the focus, neither can an item.
回答3:
I have run a test program and see no instance where FocusedItem could be nulls.
Could you post some code?
Here is what I got after I selects the item 'a' then selects the item 'b' and then click on an empty space:
ItemSelectionChanged
Item : ListViewItem: {a}
IsSelected : True
SelectedItem : ListViewItem: {a}
FocusedItem : ListViewItem: {a}
SelectedIndexChanged
SelectedItem : ListViewItem: {a}
FocusedItem : ListViewItem: {a}
ItemSelectionChanged
Item : ListViewItem: {a}
IsSelected : False
SelectedItem : null
FocusedItem : ListViewItem: {a}
SelectedIndexChanged
SelectedItem : null
FocusedItem : ListViewItem: {a}
ItemSelectionChanged
Item : ListViewItem: {b}
IsSelected : True
SelectedItem : ListViewItem: {b}
FocusedItem : ListViewItem: {b}
SelectedIndexChanged
SelectedItem : ListViewItem: {b}
FocusedItem : ListViewItem: {b}
ItemSelectionChanged
Item : ListViewItem: {b}
IsSelected : False
SelectedItem : null
FocusedItem : ListViewItem: {b}
SelectedIndexChanged
SelectedItem : null
FocusedItem : ListViewItem: {b}
Noticed that FocusedItem
is never null.
Is it the same in your case?
Would a simple if (listView.FocusedItem == null)
check do for your case?
回答4:
I had the same problem while using listview. There seems to be some problem with respect to the event fired upon selection.
SelectedIndexChanged
event where the focused item, selected items and selected indices become null. This is the reason for the above problem.ItemActivate
event can be alternatively used without any glitches where the focused item, selected items or selected indices are not null in the second or any other time.
While creating a listview with details, the SelectedIndexChanged
event is fired by default. So a change in the respective Designer class and a related event handler in the main class would do the job.
In the designer class, see the event that is subscribed. Example:
this.TaskslistView.SelectedIndexChanged
+= new System.EventHandler(TaskslistView_SelectedIndexChanged);
for which the respective TaskslistView_SelectedIndexChanged
event handler method is present in the main class. Replace this event with
this.TaskslistView.ItemActivate
+= new System.EventHandler(this.TaskslistView_ItemActivate);
and replace the respective TaskslistView_SelectedIndexChanged
with TaskslistView_ItemActivate
.
This ought to solve the problem.
回答5:
You can use ListViewItemSelectionChangedEventHandler.
ListView.ItemSelectionChanged +=
new ListViewItemSelectionChangedEventHandler(ListView_ItemSelectionChanged);
private void ListView_ItemSelectionChanged(Object sender, ListViewItemSelectionChangedEventArgs e)
{
var x = e.IsSelected;
}
ListView_ItemSelectionChanged gets called twice. You can ignore the first one by using
if (e.IsSelected == true)
edit: Try this:
ListView.Select();
来源:https://stackoverflow.com/questions/276986/listview-focuseditem-becomes-null