首先引用 Microsoft.Practices.Prismhtml
MVVM模式代碼以下:函數
XAML代碼:this
<!-- 無參方式 --> <Button Content="Test Command" Command="{Binding TestCommand}" /> <!-- 將本身做爲參數 --> <Button Content="Test Command2" Command="{Binding TestCommand2}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.Self}}" > <!-- 將父元素做爲參數 --> <Button Content="Test Command3" Command="{Binding TestCommand3}" CommandParameter="{Binding RelativeSource={x:Static RelativeSource.TemplatedParent}}" >
後臺代碼:htm
this.DataContext = new ViewModel();
ViewModel代碼:blog
// ViewModel 構造函數 public ViewModel() { CallCOmmand1 = new DelegateCOmmmand(Call1); CallCOmmand2 = new DelegateCOmmmand<Object>(Call2); CallCOmmand3 = new DelegateCOmmmand<Object>(Call3); } // 命令聲明 public DelegateCommand CallCommand { get; private set; } public DelegateCommand<Object> CallCommand2 { get; private set; } public DelegateCommand<Object> CallCommand3 { get; private set; } // 命令實現 public void Call1() { } public void Call2( Object obj ) { Button button = obj as Button; } public void Call3( Object obj ) { ParentType parent = obj as ParentType; }