UWP:如何在鼠标(指针)下获取GridView项目编号(未通过任何单击选择)?

时间:2017-09-13 16:49:43

标签: c# gridview uwp mouseover

有没有办法找出Item指针下的女巫GridView?我想在鼠标下显示有关该项目的简短信息,而无需通过任何单击选择该项目。

1 个答案:

答案 0 :(得分:2)

您可以处理PointerEntered中根元素的PointerExitedItemTemplate个事件。

<强> 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" />

<强>代码:

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;
}