Weissu
Weissu

Reputation: 419

UWP: How to get GridView Item number (not selected by any click) under the mouse (pointer)?

Is there any way to find out witch Item number is under the pointer on GridView? I like to show short information about the item under the mouse without selecting the item by any click.

Upvotes: 0

Views: 478

Answers (1)

mm8
mm8

Reputation: 169370

You could handle the PointerEntered and PointerExited events for the root element in your ItemTemplate.

XAML:

<GridView>
    <x:Int32>1</x:Int32>
    <x:Int32>2</x:Int32>
    <x:Int32>3</x:Int32>
    <GridView.ItemContainerStyle>
        <Style TargetType="GridViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            <Setter Property="VerticalContentAlignment" Value="Stretch" />
        </Style>
    </GridView.ItemContainerStyle>
    <GridView.ItemTemplate>
        <DataTemplate>
            <Grid PointerEntered="TextBlock_PointerEntered"
                          PointerExited="TextBlock_PointerExited"
                          Background="Transparent">
                <TextBlock Text="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
            </Grid>
        </DataTemplate>
    </GridView.ItemTemplate>
</GridView>

<TextBlock x:Name="tb" />

Code:

private void TextBlock_PointerEntered(object sender, PointerRoutedEventArgs e)
{
    Panel root = sender as Panel;
    var dataObject = root.DataContext;
    tb.Text = dataObject.ToString(); //displays the currently pointed number in "tb"
}

private void TextBlock_PointerExited(object sender, PointerRoutedEventArgs e)
{
    tb.Text = string.Empty;
}

Upvotes: 4

Related Questions