2011-11-24 50 views
0

我用我的应用程序的MVVM光工具包,并试图了解传递命令的参数。 我有以下XAML代码片段:RelayCommand与

<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包含的项目源,从收集的图片来。由于只有在时间的测试是我只是有1个图像。

我怎么能作为参数传递给命令,从图片这是我swPicture元单第一张照片?

暂时我能不带参数触发单个命令与模型下面的命令处理程序波纹管所:

GetInfoCommand = new RelayCommand<Picture>(
      action=> 
       { 
        GetMetaData(); 
       }, 
       g=>true); //Execute method 

的想法是,我需要从收集传递的第一张照片如为了将它传递给的getMetaData参数到我的命令,它会接受这个参数

我如何更新我的XAML代码和指令,以使工作?

回答

0

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

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

0

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"/>