0

我在我的应用程序中使用 MVVM Light 工具包并尝试了解如何传递命令。我有以下 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

这个想法是我需要将集合中的第一张图片作为参数传递给我的命令,以便将其传递给 GetMetaData,后者将接受此参数

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

4

2 回答 2

0

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

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

于 2011-11-24T17:49:09.247 回答
0

命令参数执行此操作

<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"/>
于 2015-08-13T11:00:16.617 回答