Wpf 带参数的RelayCommand

Wpf 带参数的RelayCommand,wpf,binding,mvvm,mvvm-light,relaycommand,Wpf,Binding,Mvvm,Mvvm Light,Relaycommand,我正在应用程序中使用MVVM Light toolkit,并试图学习如何传递命令。 我有以下XAML代码片段: <s:ScatterView x:Name="swPicture" ItemsSource="{Binding Pictures}" ItemTemplate="{StaticResource Scatter_Thumbnail}"/> <Button Content="Info" Width="40" Height="40"

我正在应用程序中使用MVVM Light toolkit,并试图学习如何传递命令。 我有以下XAML代码片段:

<s:ScatterView x:Name="swPicture" ItemsSource="{Binding Pictures}" ItemTemplate="{StaticResource Scatter_Thumbnail}"/>
    <Button Content="Info" Width="40" Height="40"
                         Command="{Binding GetInfoCommand}"
                           Grid.Row="0" HorizontalAlignment="Left"/>

元素swPicture包含来自图片集合的项目源。作为暂时的测试,我只有一张照片

如何将swPicture元素中图片的第一张图片作为参数传递给命令

目前,我能够在模型中使用以下命令处理程序触发单个无参数命令,如下所示:

GetInfoCommand = new RelayCommand<Picture>(
            action=>
                {
                    GetMetaData();
                },
                g=>true); //Execute method
GetInfoCommand=newrelaycommand(
动作=>
{
GetMetaData();
},
g=>true)//执行方法
其思想是,我需要将集合中的第一张图片作为参数传递给我的命令,以便将其传递给GetMetaData,GetMetaData将接受此参数


如何更新XAML代码和命令以使其正常工作?

在您的场景中,您根本不需要参数,因为您的视图模型同时具有图片集合和GetInfoCommand-GetMetaData方法可以访问集合,您可以从那里访问第一个元素


如果您的问题是如何传递参数-您可以将按钮的CommandParameter属性的值设置为某个值,或将其绑定到任何您想要的值,然后当您按下按钮时-Execute和CanExecute方法将作为参数传递该值。

CommandParameter可以这样做

<s:ScatterView x:Name="swPicture" ItemsSource="{Binding Pictures}" ItemTemplate="{StaticResource Scatter_Thumbnail}"/>
    <Button Content="Info" Width="40" Height="40"
                         Command="{Binding GetInfoCommand}" CommandParameter="{Binding Pictures[0]}"
                           Grid.Row="0" HorizontalAlignment="Left"/>