0

I have a ListView in a user control:

<ListView ...
          ItemsSource="{Binding Path=MyItemsSource}"
          SelectedItem="{Binding Path=SelectedItem}"
          SelectionMode="{Binding Path=SelectionMode}"
          >
    <ListView.View>
        <GridView>
            ...
        </GridView>
    </ListView.View>
</ListView>

I have code in the viewmodel to programmatically set the SelectedItem of the ListView. This is achieved by setting the SelectedItem property in the viewmodel.

I have found that when my code sets the SelectedItem property to a particular list item, the item that has keyboard focus is not changed along with it. If I change the SelectedItem property and then press the up arrow key, the newly-selected item is the one that is above the item that was selected before (because that item still has the focus), not the item above the newly-selected item.

The chosen answer in this question suggests to use code like the following:

ListViewItem item = myListView.ItemContainerGenerator.ContainerFromIndex(index) as ListViewItem;

However, that solution does not compile for me. I get the following error:

CS0039: Cannot convert type 'System.Windows.DependencyObject' to 'System.Windows.Forms.ListViewItem' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.

I have already worked around an issue described in this question that relates to the wrong list item being used as the start point for the multi selection when the user shift-clicks, after programmatically setting the selected item. This is worked around by changing the SelectionMode before and after setting the selected item:

private MyItemType _selectedItem;
public MyItemType SelectedItem
{
    get
    {
        return _selectedItem;
    }
    set // Use SetSelectedItemInternal() internally!
    {
        SetProperty(ref _selectedItem, value);
    }
}

/// <summary>
/// Use this when programatically setting the SelectedItem. This method incorporates a workaround for a bug (?) in WPF
/// that causes confusing behaviour when shift-selecting items in the list after the SelectedItem is programatically changed.
/// See https://stackoverflow.com/questions/11950021/wpf-listview-shift-selecting-multiple-items-wrong-start-item
/// </summary>
private void SetSelectedItemInternal(MyItemType newSelectedItem, bool scrollToNewItem = true)
{
    SelectionMode = SelectionMode.Single;
    SelectedItem = newSelectedItem;
    SelectionMode = SelectionMode.Extended;

    if (scrollToNewItem)
    {
        ScrollToListItem(MyItemsSource.IndexOf(newSelectedItem));
    }
}
Community
  • 1
  • 1
Hammerite
  • 21,755
  • 6
  • 70
  • 91
  • What your question is about is implicit in both the tags and the fact that you have XAML. – H.B. Jul 12 '16 at 15:12
  • There's a difference between it being clear to a careful reader which ListView the question is about, and it being difficult to overlook. – Hammerite Jul 12 '16 at 15:37
  • Well, it's really difficult to overlook, there is no XAML in winforms. Also, a lot of people that answer questions have a few favourite tags they check out, so they may not even see winforms questions. And if they browse both tags they probably make sure to check what it is about if not obvious. – H.B. Jul 12 '16 at 15:40

1 Answers1

0

CS0039: Cannot convert type 'System.Windows.DependencyObject' to 'System.Windows.Forms.ListViewItem'

You have a wrong using statement (referencing the windows forms namespace) in your code. Also, just cast to DependencyObject, as that apparently is what you list contains. You should then be able to focus that, though UI virtualization will thwart that if the new selected item is off-screen.

H.B.
  • 166,899
  • 29
  • 327
  • 400