Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
336 views
in Technique[技术] by (71.8m points)

c# - Mvvm Light UWP Index Of Listitem When Button In ListItem Is Pressed

My issue:

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:

One ListItem

The code in 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>

The command:

    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 answer to this question needs to provide:

  • How to get the index of the ListItem which is containing the AppBarButton which has been pressed for being used in the relaycommand (be aware the ListItem doesn't get selected when the user is pressing the AppBarButton)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 =>
{
    ....
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...