简体   繁体   中英

How to detect click in GridView

I have a GridView, and in this Gridview there are several Grids which contain other elements. If I click onto one element of my GridView (one Grid) I want to open details about this element, but Grid and GridView dont seem to support setting a click method. What else can I do to call a method when an element is clicked?

but Grid and GridView dont seem to support setting a click method

Use Tapped event.

您可以添加一个不可见的矩形或其他元素来注册OnMouseOver事件,或单击ect。

Each of your Grid should subscribe to 3 events and have transparent background:

 <Grid Background="Transparent" PointerPressed="Grid_OnPointerPressed" PointerReleased="Grid_OnPointerReleased" PointerExited="Grid_OnPointerExited">

in code you may simulate click event like this:

    private bool _isPressed;

    private void Grid_OnPointerPressed(object sender, PointerRoutedEventArgs e)
    {
        _isPressed = true;
    }

    private void Grid_OnPointerReleased(object sender, PointerRoutedEventArgs e)
    {
        if (_isPressed)
        {
            //your logic on click event
        }

        _isPressed = false;
    }

    private void Grid_OnPointerExited(object sender, PointerRoutedEventArgs e)
    {
        _isPressed = false;
    }

or the easiest way is to wrap your Grid into Button

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM