UWP: how to get the RightTapped GridView Item

后端 未结 2 1940
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 08:21

I have a GridView like below



        
2条回答
  •  悲哀的现实
    2021-01-28 08:53

    You should set SelectionMode="Single"in gridview and set IsRightTapEnabled="True".

    My model has a class Student:

    public class Student : NotifyProperty
    {
        public string Name
        {
            set
            {
                _name = value;
                OnPropertyChanged();
            }
            get
            {
                return _name;
            }
        }
    
        private string _name;
    }
    

    And my viewmodel has a List.

    I set the list as gridview's source.

    My gridview is

       
            
                
                    
                    
            
        
    

    The SymbolGridView_OnRightTapped have a OriginalSource that is TextBlock.But if your DataTemlpate is a Grid ,the OriginalSource is Grid.

    We can use var student = (e.OriginalSource as TextBlock)?.DataContext as Student; to get the student.

    The OriginalSource.DataContext is your selects item

    But if you use the Grid,the OriginalSource may is ListViewItemPresenter.So the easy methor is use var student = (e.OriginalSource as FrameworkElement)?.DataContext as Student;

    See: http://lindexi.oschina.io/lindexi/post/win10-uwp-%E5%8F%B3%E5%87%BB%E9%80%89%E6%8B%A9GridViewItem

提交回复
热议问题