UWP [C ++]右键单击gridview中的项目时如何获取Gridview选择的ItemIndex。 [英] UWP [C++] How to get the Gridview selected ItemIndex when right click on an item in gridview.

查看:71
本文介绍了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;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP; ItemContainerStyle = QUOT; {StaticResource的GridContainerStyle}"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;的SelectionMode = QUOT;单"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; IsRightTapEnabled = QUOT;真"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; IsItemClickEnabled = QUOT;真"


                 的SelectionChanged = QUOT; GridView_SelectionChanged"

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;项目单击= QUOT; GridView_ItemClick" >

            < GridView.ItemTemplate>&
                < DataTemplate>&
                    < Grid RightTapped =" CustomerGridView_RightTapped"宽度= QUOT; 256"高度= QUOT; 130"保证金="5,5,5,5">&
               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < StackPanel Grid.Row =" 0"背景= QUOT;灰色+ QUOT;宽度= QUOT; 256"高度= QUOT; 130">

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < Image Source =" Assets / icon.png"宽度= QUOT; 65 QUOT;高度= QUOT; 65 QUOT; />

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; < / StackPanel的>

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP;&NBSP; < StackPanel Width =" 256"高度= QUOT; 30英寸;保证金="0,100,0,0">

                &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; < TextBlock x:Name =" customerText" Text =" {Binding Customers}" />

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP;&NBSP; < / StackPanel的>

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&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"获取索引  ?

But don't know how to get the Index with "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 I adds 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。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆