UWP [C ++]右键单击gridview中的项目时如何获取Gridview选择的ItemIndex。

编程入门 行业动态 更新时间:2024-10-22 05:16:53
本文介绍了UWP [C ++]右键单击gridview中的项目时如何获取Gridview选择的ItemIndex。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用下面的XAML获取右键单击/选项卡上所选Gridview项目的索引。

I need to obtain the Index of the selected Gridview item on right click/tab with my XAML below.

我只能在GridView" SelectionChanged"时才能获得索引。但不是"RightTapped"事件。

I was able to get the index only when GridView "SelectionChanged" but not on "RightTapped" event.

有谁能告诉我如何实现这个目标?

Can anyone show me how to achieve this ?

谢谢

XAML:

< GridView的X:名称= QUOT; CustommerGridView" &NBSP ; ItemContainerStyle = QUOT; {StaticResource的GridContainerStyle}" 的SelectionMode = QUOT;单" IsRightTapEnabled = QUOT;真" IsItemClickEnabled = QUOT;真" 的SelectionChanged = QUOT; GridView_SelectionChanged" 项目单击= QUOT; GridView_ItemClick" > < GridView.ItemTemplate>& < DataTemplate>& < Grid RightTapped =" CustomerGridView_RightTapped"宽度= QUOT; 256"高度= QUOT; 130"保证金="5,5,5,5">& < StackPanel Grid.Row =" 0"背景= QUOT;灰色+ QUOT;宽度= QUOT; 256"高度= QUOT; 130"> < Image Source =" Assets / icon.png"宽度= QUOT; 65 QUOT;高度= QUOT; 65 QUOT; /> &NBSP ; < / StackPanel的> &NBSP ; < StackPanel Width =" 256"高度= QUOT; 30英寸;保证金="0,100,0,0"> < TextBlock x:Name =" customerText" Text =" {Binding Customers}" /> &NBSP ; < / StackPanel的> &NBSP ; < / Grid>& < / DataTemplate>& < /GridView.ItemTemplate> < / GridView>

<GridView x:Name="CustommerGridView" ItemContainerStyle="{StaticResource GridContainerStyle}" SelectionMode="Single" IsRightTapEnabled="True" IsItemClickEnabled="True" SelectionChanged="GridView_SelectionChanged" ItemClick="GridView_ItemClick" > <GridView.ItemTemplate> <DataTemplate> <Grid RightTapped="CustomerGridView_RightTapped" Width="256" Height="130" Margin="5,5,5,5"> <StackPanel Grid.Row="0" Background="Gray" Width="256" Height="130"> <Image Source="Assets/icon.png" Width="65" Height="65" /> </StackPanel> <StackPanel Width="256" Height="30" Margin="0,100,0,0"> <TextBlock x:Name="customerText" Text="{Binding Customers}" /> </StackPanel> </Grid> </DataTemplate> </GridView.ItemTemplate> </GridView>

获得带有"SelectionChanged"的索引没问题。

Got the index with "SelectionChanged" no problem.

void MainPage :: GridView_SelectionChanged(Platform :: Object ^ sender, Windows :: UI :: Xaml :: Controls :: SelectionChangedEventArgs ^ e ) { GridView ^ gridview = safe_cast< GridView ^>(发件人); int SelectedIndex = gridview-> SelectedIndex; }

void MainPage::GridView_SelectionChanged(Platform::Object^ sender, Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e) { GridView^ gridview = safe_cast<GridView^>(sender); int SelectedIndex = gridview->SelectedIndex; }

但是不知道如何使用"CustomerGridView_RightTapped"获取索引 ?

Butdon't know how to get the Indexwith "CustomerGridView_RightTapped" ?

void MainPage :: CustomerGridView_RightTapped(Platform :: Object ^ sender,Windows :: UI :: Xaml :: Input :: RightTappedRoutedEventArgs ^ e) { Grid ^ grid = safe_cast< Grid ^>(发件人);

void MainPage::CustomerGridView_RightTapped(Platform::Object^ sender, Windows::UI::Xaml::Input::RightTappedRoutedEventArgs^ e) { Grid^ grid = safe_cast<Grid^>(sender);

//如果我在下面添加会有例外吗?

//Got exception if Iadds below ?

// GridView ^ gridview = safe_cast< GridView ^>(发件人); // int SelectedIndex = gridview-> SelectedIndex;

//GridView^ gridview = safe_cast<GridView^>(sender); //int SelectedIndex = gridview->SelectedIndex;

推荐答案

嗨dalee,

Hi dalee,

试试这个;

void MainPage::CustomerGridView_RightTapped(Platform::Object^ sender, Windows::UI::Xaml::Input::RightTappedRoutedEventArgs^ e) { Grid^ grid = safe_cast<Grid^>(sender); Object^ dataObject = grid->DataContext; // Data bound to the item // Replace class name "CustomerData" below to the actual class name. auto data = safe_cast<CustomerData^>(dataObject); auto items = safe_cast<Windows::Foundation::Collection::IVector<CustomerData^>^>(CustommerGridView->ItemsSource); // Get index unsigned int SelectedIndex; items->IndexOf(data, &SelectedIndex); }

更多推荐

UWP [C ++]右键单击gridview中的项目时如何获取Gridview选择的ItemIndex。

本文发布于:2023-11-10 16:02:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575824.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:右键   单击   项目   UWP   ItemIndex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!