4

I am trying to detect which item in a listview is focused, but I am not getting the events detected. I am developing for Xbox One UWP, so I cannot use mouse or keyboard events, only focus can be used.

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel Orientation="Vertical" GotFocus="StackPanel_GotFocus" >
            <StackPanel Name="Imagestack" Orientation="Horizontal">
                <Image Source="{Binding Image}" Height="144" Width="256" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding Title}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </StackPanel>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>
private void StackPanel_GotFocus(object sender, RoutedEventArgs e)
{
    Debug.WriteLine("Image focus");
    Image img = sender as Image;
    Bgimage.Source = img.Source;
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • Your sender is not Image. Its Stackpanel. You have to walk your way down to the Image within the StackPanel – lokusking Feb 22 '17 at 12:20
  • Thanks for pointing the mistake. But the event is not getting detecting, i dont get the log. –  Feb 22 '17 at 12:30
  • I don't think a stackpanel **can** get focus. What would it even do with it? – Manfred Radlwimmer Feb 22 '17 at 12:41
  • *Controls* are focusable, and *StackPanel* derives from *Panel* class. – Romasz Feb 22 '17 at 21:07
  • What do you want to achieve - what do you mean by focused? Does it mean selected? If so then why not to use *SelectionChanged* event of ListView? If by focus you mean highlighting the element before selsection, then maybe you can use *Pointer* events for that - for example subscribe to *StackPanel's* *PointerEntered* (lost/canceled) events depending on your needs. – Romasz Feb 22 '17 at 21:18
  • I am developing for xboxone platform and all navigation are based on focus. That is why i am not using select item. –  Feb 23 '17 at 04:58

2 Answers2

0

You should register to the ListView.GotFocus event.

The OriginalSource from the event parameter will be the ListViewItem which has received the focus. You can then retrieve the item content using ListViewItem.Content.

XAML:

<ListView x:Name="list" GotFocus="list_GotFocus">
     <ListView.ItemTemplate>...</ListView.ItemTemplate>
</ListView>

Code behind:

private void list_GotFocus(object sender, RoutedEventArgs e)
{
     var focusedItem = (e.OriginalSource as ListViewItem)?.Content; 
}
Vincent
  • 3,656
  • 1
  • 23
  • 32
  • Thanks for the help. I completed by MyListItem focusedItem = (MyListItem)(e.OriginalSource as ListViewItem)?.Content; Debug.WriteLine("focusedItem " + focusedItem.Title); Debug.WriteLine("focusedItem " + focusedItem.index); var temp = focusedItem.Image; Bgimage.Source = (ImageSource)temp; –  Feb 23 '17 at 05:03
-1

You don't need to get focus state to get data from the clicked ListViewItem, the ItemClick event of the ListView may be what you're looking for:

<ListView x:Name="LV_Items"
          IsItemClickEnabled="True"
          ItemClick="LV_Items_ItemClick"
          >
</ListView>

private void LV_Items_ItemClick(object sender, ItemClickEventArgs e)
{
    // Get instance of the model in the clicked ListViewItem
    MyModel myModel = (MyModel)e.ClickedItem;

    Image img = myModel.Image;
}
Marian Dolinský
  • 3,312
  • 3
  • 15
  • 29
  • I cannot use click because i need focus (xbox one platform - navigation). Thanks for the help –  Feb 23 '17 at 05:01
  • Have you tried it on real xbox? I haven't got a chance but I doubt that you have to track the focus to get the selected item.. – Marian Dolinský Feb 23 '17 at 13:07
  • Yeah i tried i Xbox and used focus to track the navigation –  Feb 24 '17 at 02:14