2014-01-06 53 views
0

我有一個函數應用於silverlight數據網格中複選框的check和unchecked事件。我在xaml.cs中創建了這個函數。我如何在ViewModel中做同樣的功能?在ViewModel中處理CheckBox的Checked和Uncheck事件Silverlight MVVM

主要問題是我需要在ViewModel中訪問發件人。

private void HandleChecked(object sender, RoutedEventArgs e) 
    { 
     CheckBox chk = sender as CheckBox; 
     var bindingExpression = chk.GetBindingExpression(CheckBox.IsCheckedProperty); 
     if (bindingExpression != null) 
     { 
      bindingExpression.UpdateSource(); 
     } 
    } 

回答

2

你可以讓你的視圖模型(即MyViewModel.cs)的方法:

public void DoCheckboxStuff(bool checked) 
{ 
    ... 
} 

,並從您的視圖訪問它(.xaml.cs):

private void HandleChecked(object sender, RoutedEventArgs e) 
{ 
    CheckBox chk = sender as CheckBox; 
    MyViewModel mvm = this.DataContext as MyViewModel; 
    mvm.DoCheckboxStuff(chk.IsChecked); 
} 
相關問題