当按下ListItem中的按钮时,Mvvm Light UWP Listitem索引 [英] Mvvm Light UWP Index Of Listitem When Button In ListItem Is Pressed

查看:74
本文介绍了当按下ListItem中的按钮时,Mvvm Light UWP Listitem索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:

我有一个带有ItemTemplate的ListView;包含其中带有一些AppBarButtons的DataTemplate. 现在,当用户在ListView中按下按钮之一时,包含按钮的ListItem不会突出显示. 这个问题也导致了我不知道如何得到的问题 listitem的索引,其中包含用户单击的按钮.

I got a ListView which has a ItemTemplate; containing a DataTemplate with some AppBarButtons in it. Now when the user is pressing one of the Buttons in the ListView the ListItem which is containing the button doesn't get highlighted. This Problem also leads to the issue that I don't know how to get the index of the listitem which is containing the button the user clicked.

ListItem:

Mainpage.xaml中的代码:

       <ListView.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical">                                 
                                <StackPanel Orientation="Horizontal"> 
                                           <!-- Simplified, Left out other AppBarButtons -->
                                            <AppBarButton Foreground="White" VerticalAlignment="Center" 
                                              Label="Navigate" Icon="Map" >
                                                <interactivity:Interaction.Behaviors>
                                                    <core:EventTriggerBehavior EventName="Tapped">
                                                        <core:InvokeCommandAction Command="{Binding Main.OpenMapCommand, Mode=OneWay, Source={StaticResource Locator}}"></core:InvokeCommandAction>
                                                    </core:EventTriggerBehavior>
                                                </interactivity:Interaction.Behaviors>
                                            </AppBarButton>                                      
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
        </ListView.ItemTemplate>

命令:

    public RelayCommand OpenMapCommand
    {
        get
        {
            return _openMapCommand
                       ?? (_openPaneCommand = new RelayCommand(
                           () =>
                           {
                               Debug.WriteLine("Opening map");
                               // Center on New York City
                               var uriNewYork = new Uri(@"bingmaps:?cp=40.726966~-74.006076");

                               // Launch the Windows Maps app
                               var launcherOptions = new Windows.System.LauncherOptions();
                               launcherOptions.TargetApplicationPackageFamilyName = "Microsoft.WindowsMaps_8wekyb3d8bbwe";
                               var success = Windows.System.Launcher.LaunchUriAsync(uriNewYork, launcherOptions);
                           }));
                   }
    }

此问题的Whatan答案需要提供:

Whatan answer to this question needs to provide:

  • 如何获取包含已在中继命令中按下的AppBarButton的ListItem的索引(请注意,当用户按下AppBarButton时未选择ListItem )

推荐答案

ItemTemplate中的DataContext将是您要作为命令参数传递的项目,因此您应该能够将其绑定到CommandParameter:

The DataContext within the ItemTemplate will be the item that you want to pass as a command parameter, so you should be able to just bind it to CommandParameter:

<core:InvokeCommandAction Command="{Binding Main.OpenMapCommand, Source={StaticResource Locator}}" CommandParameter="{Binding}"/>

_openPaneCommand = new RelayCommand(item =>
{
    ....
});

这篇关于当按下ListItem中的按钮时,Mvvm Light UWP Listitem索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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