开发者

How to make the command connection in silverlight MVVM?

I trying to use MVVM in some silverlight modal that i wrote - I wro开发者_高级运维te the view - and the viewmodel part - but i need to make the command between them and i don't know how to do it.

In the view i have single button that will launch the command.

How to do it ?

Thanks for the help.


In View Model

private RelayCommand _Command;
public RelayCommand Command
{
  get
  {
    if (_Command == null)
    {
      _Command= new RelayCommand(() =>
      {
      });
    }
    return _Command;
  }
  private set { }
}

USE PARAMETERS

private RelayCommand<string> _Command;
public RelayCommand<string> Command
{
  get
  {
    if (_Command == null)
    {
      _Command= new RelayCommand<string>((X) =>
      {
      });
    }
    return _Command;
  }
  private set { }
}

In View

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" xmlns:gs_cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.SL4"

<Button  Grid.Row="1" Grid.Column="1" Margin="4" HorizontalAlignment="Right" Name="btnSelect" Content="..." Width="25" Height="25" TabIndex="2">
                                        <i:Interaction.Triggers>
                                            <i:EventTrigger EventName="Click">
                                                <gs_cmd:EventToCommand Command="{Binding Path=Command,Mode=OneWay}"/>
                                            </i:EventTrigger>
                                        </i:Interaction.Triggers>
                                    </Button>


Another version with Parameters, to add to Masoomian's anser:

private RelayCommand<MyViewModel> _Command; 
public RelayCommand<MyViewModel> Command 
{ 
  get 
  { 
    if (_Command == null) 
    { 
      _Command= new RelayCommand<MyViewModel>((vm) => 
      { 
        vm.IsBusy = true; // Set a Parameter
        vm.DoSomething(); // Do some work
        // Call other methods on the View Model as needed
        // ...
      }); 
    } 
    return _Command; 
  } 
  private set { } 
} 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜