如何在正确的事件中获取所选gridview项的id? [英] How to get id of selected gridview item in righttapped event?

查看:103
本文介绍了如何在正确的事件中获取所选gridview项的id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的uwp应用程序工作正常,但仍有一个尚未解决的问题。右键单击gridview项会打开一个menuflyout。一些menuflyout项必须折叠或可见,这就是我需要数据库表记录的Id的原因。调试应用程序DataContext显示
Id,但我无法使用它。在代码(resepti.Id = 192)中,已手动输入Id的值,并且menuflyout正常工作。

My uwp app works fine but there is still one unresolved problem. Right clicking gridview item opens a menuflyout. Some menuflyout items must be collapsed or visible and that's why I need Id of the database table record. Debugging the app DataContext shows the Id but I can't use it. In the code (resepti.Id = 192) value of Id has been entered manually and the menuflyout works.

<GridView x:Name="ReseptiGridView" Grid.Column="0" ScrollViewer.VerticalScrollBarVisibility="Auto"
                            ItemsSource="{Binding Mode=OneWay, Source={StaticResource ReseptitViewSource}}" 
                            ItemTemplate="{StaticResource ReseptiTemplate}"
                            IsItemClickEnabled="True"
                            ItemClick="ReseptiGridView_ItemClick"                           
                            Holding="ReseptiGridView_Holding" VerticalAlignment="Stretch"
                            RightTapped="ReseptiGridView_RightTapped" Margin="0,20,0,0" SelectionMode="Single">
                    <GridView.ContextFlyout>
                        <MenuFlyout x:Name="recipeMenuFlyout" Placement="Right"  MenuFlyoutPresenterStyle="{StaticResource MenuFlyoutPresenterStyle}">
                            <MenuFlyoutItem x:Name="addRecipe" x:Uid="AddRecipe" Click="AddRecipe_Click"/>
                            <MenuFlyoutItem x:Name="editRecipe" x:Uid="EditRecipe" Click="EditRecipe_Click"/>
                            <MenuFlyoutItem x:Name="deleteRecipe" x:Uid="DeleteRecipe" Click="DeleteRecipe_Click"/>
                            <MenuFlyoutSubItem x:Name="addImage" Text="Add picture">
                                <MenuFlyoutItem x:Uid="AddImageFile" Text="File" Click="AddImageFile_Click" />
                                <MenuFlyoutItem x:Uid="AddImageCopy" Text="Copy" Click="AddImageCopy_Click" />
                            </MenuFlyoutSubItem>
                            <MenuFlyoutItem x:Name="deleteImage" x:Uid="DeleteImage" Click="DeleteImage_Click">
                            </MenuFlyoutItem>
                        </MenuFlyout>
                    </GridView.ContextFlyout>
                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <local:WrapPanel />
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                </GridView>





private void ReseptiGridView_RightTapped(object sender, RightTappedRoutedEventArgs e)
        {
            ReseptiGridView.SelectedItem = (e.OriginalSource as FrameworkElement).DataContext;
            //VariablesRes.indexRes = ReseptiGridView.SelectedIndex;
            resepti = new ReseptiViewModel();
            reseptiViewModel = new ReseptiViewModel();
            resepti.Id = 195;
            resepti = reseptiViewModel.GetResepti(resepti.Id);

            if (reseptit.Count != 0)
            {
                if (ReseptiGridView.SelectedItem != null)
                {                    
                    if (resepti.Kuva == null || resepti.Kuva == "")
                    {
                        addRecipe.Visibility = Visibility.Visible;
                        editRecipe.Visibility = Visibility.Visible;
                        deleteRecipe.Visibility = Visibility.Visible;
                        addImage.Visibility = Visibility.Visible;
                        deleteImage.Visibility = Visibility.Collapsed;
                        recipeMenuFlyout.ShowAt(ReseptiGridView, e.GetPosition(ReseptiGridView));
                    }
                    else
                    {






$

推荐答案

你好,

你不应该在这里使用SelectionItem,因为右键是与selectedindex或selecteditem不同,selectedindex和selecteditem都用作左键单击。我建议你这样做:

You should not use SelectionItem here since right tap is different from selectedindex or selecteditem, both selectedindex and selecteditem are used as left click. I would recommed you do this:

1。我相信你有一个集合,你将这个集合绑定到你的GridView。我假设您已经设置了一个唯一的属性,以便您可以从其他项目中识别它。

1. I believe you have a collection and you are binding this collection to your GridView. And I assume you have set a property that can be unique so that you can identify it from other items.

2。从DataContext获取信息时。这是一个模型,你可以转换。假设您的模型是成员,您的收藏是成员。然后 

2. When you get the info from your DataContext. It's a model and you can convert. Say your model is member and your collection is members. Then 

var x=(e.OriginalSource as FrameworkElement).DataContext;
            var identity = (x as Member).Alias;

然后您可以从您的收藏中查询您的身份,然后您就会理解它的索引。这就是你真正需要的ID。 

Then you can query your identity from your collection and then you will understand it's index. And that is the ID you really need. 

这对你有用吗?

祝你好运,

Barry


这篇关于如何在正确的事件中获取所选gridview项的id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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