【问题标题】:Mvvm Light UWP Index Of Listitem When Button In ListItem Is Pressed按下 ListItem 中的按钮时,Mvvm Light UWP Listitem 索引
【发布时间】:2017-03-13 14:46:30
【问题描述】:

我的问题:

我有一个 ListView,它有一个 ItemTemplate;包含一个带有一些 AppBarButtons 的 DataTemplate。 现在,当用户按下 ListView 中的按钮之一时,包含该按钮的 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);
                           }));
                   }
    }

这个问题需要提供什么答案:

  • 如何获取包含 AppBarButton 的 ListItem 的索引,该 AppBarButton 已被按下以便在 relaycommand 中使用(请注意,当用户按下 AppBarButton 时,ListItem 不会被选中 )

【问题讨论】:

    标签: c# xaml listview uwp mvvm-light


    【解决方案1】:

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

    <core:InvokeCommandAction Command="{Binding Main.OpenMapCommand, Source={StaticResource Locator}}" CommandParameter="{Binding}"/>
    
    _openPaneCommand = new RelayCommand(item =>
    {
        ....
    });
    

    【讨论】:

      【解决方案2】:

      一个常见的场景是使用按钮上的 Tag 属性,绑定到 ListItem 的值并将其作为命令参数传递

      查看其他 StackO 答案了解详情UWP buttons inside Listview items

      【讨论】:

        猜你喜欢
        • 2011-02-22
        • 2013-12-01
        • 1970-01-01
        • 2012-02-07
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        • 2016-01-06
        • 2015-11-02
        相关资源
        最近更新 更多