我正在尝试创建一个菜单,当您在GridView中点击并按住某个项目时会出现该菜单。该菜单有一个从GridView源中删除项目的选项。当我点击“删除”选项时,如何引用最初点击的项目?这是Xaml:
<GridView Name="SharedBooksGrid" ScrollViewer.VerticalScrollBarVisibility="Hidden" ItemClick="BookClicked" IsItemClickEnabled="True" Grid.Row="3" Holding="SharedHold" RightTapped="RightTapHold">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="250" Width="400">
<Image Source="{Binding BookImage}" Stretch="UniformToFill"/>
<Rectangle Opacity="0.35" Fill="Black" Height="60" VerticalAlignment="Top" />
<TextBlock Text="{Binding BookName}" FontSize="29" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" Foreground="White" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<FlyoutBase.AttachedFlyout>
<MenuFlyout x:Name="SharedMenu">
<MenuFlyoutItem Text="Make Default" />
<MenuFlyoutSeparator />
<MenuFlyoutItem Text="Delete" Click="DeleteShared_Tapped" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
</GridView>
背后的代码:
private Book deleteAccessBook;
private void RightTapHold(object sender, RightTappedRoutedEventArgs e)
{
Point point = e.GetPosition(sender as UIElement);
SharedMenu.ShowAt(sender as UIElement, point);
}
private async void DeleteShared_Tapped(object sender, RoutedEventArgs e)
{
deleteAccessBook = e.OriginalSource as Book;
bookAccessCollection = await BookAccessTable.ToCollectionAsync();
foreach (var item in bookAccessCollection)
{
if (item.UserId == App.MobileService.CurrentUser.UserId)
{
if (item.BookId == deleteAccessBook.id)
{
await BookAccessTable.DeleteAsync(item);
}
}
}
}
我的想法是e.OriginalSource将包含被点击的项目,但是当尝试从deleteAccessBook中查找id时会抛出异常。思考?谢谢!
答案 0 :(得分:2)
我认为你应该能够通过获取holded元素的 DataContext 来获取 Book :
<GridView Name="SharedBooksGrid" ScrollViewer.VerticalScrollBarVisibility="Hidden" ItemClick="BookClicked" IsItemClickEnabled="True" Grid.Row="3" Holding="SharedHold">
<GridView.ItemTemplate>
<DataTemplate>
<Grid Height="250" Width="400" RightTapped="RightTapHold">
<Image Source="{Binding BookImage}" Stretch="UniformToFill"/>
<Rectangle Opacity="0.35" Fill="Black" Height="60" VerticalAlignment="Top" />
<TextBlock Text="{Binding BookName}" FontSize="29" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" Foreground="White" />
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
<FlyoutBase.AttachedFlyout>
<MenuFlyout x:Name="SharedMenu">
<MenuFlyoutItem Text="Make Default" />
<MenuFlyoutSeparator />
<MenuFlyoutItem Text="Delete" Click="DeleteShared_Tapped" />
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
</GridView>
private Book deleteAccessBook;
private void RightTapHold(object sender, RightTappedRoutedEventArgs e)
{
var holdedElement = e.OriginalSource as FrameworkElement;
if (holdedElement == null) return;
deleteAccessBook = holdedElement.DataContext as Book;
SharedMenu.ShowAt(holdedElement);
}
private async void DeleteShared_Tapped(object sender, RoutedEventArgs e)
{
if (deleteAccessBook == null) return;
bookAccessCollection = await BookAccessTable.ToCollectionAsync();
foreach (var item in bookAccessCollection)
{
if (item.UserId == App.MobileService.CurrentUser.UserId)
{
if (item.BookId == deleteAccessBook.id)
{
await BookAccessTable.DeleteAsync(item);
}
}
}
}