【问题标题】:UWP: How to get GridView Item number (not selected by any click) under the mouse (pointer)?UWP:如何获取鼠标(指针)下的GridView项目编号(未通过任何点击选择)?
【发布时间】:2018-02-22 11:39:25
【问题描述】:

有什么方法可以找出女巫Item 的号码在GridView 的指针下吗?我喜欢在鼠标下显示有关该项目的简短信息,而无需通过任何单击选择该项目。

【问题讨论】:

    标签: c# gridview uwp mouseover


    【解决方案1】:

    您可以为ItemTemplate 中的根元素处理PointerEnteredPointerExited 事件。

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

    【讨论】:

    • 天啊!如此简单但有效。我完全没有意识到我可以使用这些事件,例如PointerEntered ect inside DataTemplate。你给出的例子再清楚不过了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多