列表视图类控件,如ListView/GridView,有时项目需要按下右键浮出选项,来获取Item的DataContext。
下面的示例代码,事先我已经有了一个自定义类Video,并且已经绑定了数据源。
xaml:
<ListView.ItemTemplate>
<DataTemplate x:DataType="modles:Video">
<Grid x:Name="temporaryContent_gird" RightTapped="temporaryContent_gird_RightTapped">
<TextBlock TextWrapping="WrapWholeWords" TextTrimming="CharacterEllipsis" Text="{x:Bind Video_Title}"/>
<Grid.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem x:Name="removeTemporary_item" Text="Remove" Click="removeTemporary_item_Click"/>
</MenuFlyout>
</Grid.ContextFlyout>
</DataTemplate>
</ListView.ItemTemplate>
后台代码:
鼠标右键点击事件
using Windows.UI.Xaml; private FrameworkElement sender_value; private void temporaryContent_gird_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
sender_value = (FrameworkElement)sender;
}
按下浮出选项:
private void removeTemporary_item_Click(object sender, RoutedEventArgs e)
{
var delete_temporaryVideo = sender_value.DataContext as Video;
}