Aak*_*sha 5 c# gridview selecteditem uwp
我正面临着GridView Control的问题.我们在8.1上有一个可用的Windows应用商店应用,其中GridView左右键单击具有不同的功能.在鼠标左键单击的情况下,我们曾经使用"ItemClick"事件执行导航到另一个XAML页面.在右键单击GridItem时,它会被选中并显示appbar,我们为此使用了"SelectionChanged"事件.
我们现在将现有的Windows应用商店应用程序迁移到UWP应用程序,我们使用了相同的gridView代码,我们发现功能和外观上有显着差异,我们没有看到如上图所示的GridView项目选择.我们看到"ItemClick"和"SelectionChanged"正在协同工作.在左键单击项目时,流程类似于此,控件转到SelectionChanged事件,然后转到ItemClick.我们无法区分左鼠标单击和鼠标右键单击等操作,因为点击左键单击/点击后两个事件都会被激活.我们在鼠标左键和右键上有不同的功能.
需要有关如何在UWP中模仿Windows 8.1功能的帮助.
我的要求是我想使用右键单击/长按来选择一个项目并从应用程序栏按钮采取相应的操作,并在左键单击/点击应该重定向到下一个XAML页面.我遇到的问题是右键单击,我无法检测到哪些GridView项被单击,我如何将其添加到SelectedItem中.
我做的是,我在GridView的DataTemplate中引入了额外的Grid.在这个Grid中,我添加了RightTapped事件.
示例代码段是
<GridView x:Name="ItemGridView"
ItemsSource="{Binding Source={StaticResource ItemsViewSource}}"
IsItemClickEnabled="True"
SelectionMode="Single" ItemClick="ItemGridView_ItemClick"
SelectionChanged="ItemGridView_SelectionChanged">
<GridView.ItemTemplate>
<DataTemplate>
<Grid RightTapped="Grid_RightTapped">
<Border Background="White" BorderThickness="0" Width="210" Height="85">
<TextBlock Text="{Binding FileName}" />
</Border>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
Run Code Online (Sandbox Code Playgroud)
事件名称为Grid_RightTapped.这有助于我检测到GridViewItem,我得到了长按/右键单击.
这个代码隐藏是:
private void Grid_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
Song selectedItem = (sender as Grid).DataContext as Song;
//the above line will get the exact GridViewItem where the User Clicked
this.ItemGridView.SelectedItem = selectedItem;
//the above line will add the item into SelectedItem and hence, I can take any action after this which I require
}
}
Run Code Online (Sandbox Code Playgroud)
我们这样做的原因是,因为现在我们可以使用右键单击将Clicked项添加到GridView SelectedItem中.现在在UWP中,单击的项目仅使用左键单击添加到SelectedItem中.左键单击,我可以使用ItemClick事件导航到另一个页面.