2012-07-25 39 views
2

如何添加ICommand來自FrameworkElement的活動?如何將ICommand添加到FrameworkElement中的事件?

具體來說,我想要做以下

<ListView> <!-- Button, Windows or whatever element --> 
    <my:EventToCommand 
     Name="PreviewMouseLeftButton" 
     Command="{Binding Path=MyCommand}" 
     CommandParameter="{Binding ...}" /> 
    <!-- Value, Element, etc.. --> 
</ListView> 

我想要實現我自己的解決方案,這是對教育目的,我不希望使用任何第三方庫(MVVM光,棱鏡,等)

回答

5

您需要使用附加行爲。出於演示的目的,說我想要實現使用MVVM和ICommand的模式按鈕雙擊,這裏的相關代碼:

首先,創建一個名爲ButtonBehaviors靜態類,看起來像這樣:

public static class ButtonBehaviors 
{ 
    public static object GetButtonDoubleClick(DependencyObject obj) 
    { 
     return obj.GetValue(ButtonDoubleClickProperty); 
    } 

    public static void SetButtonDoubleClick(DependencyObject obj, object value) 
    { 
     obj.SetValue(ButtonDoubleClickProperty, value); 
    } 

    public static readonly DependencyProperty ButtonDoubleClickProperty = 
     DependencyProperty.RegisterAttached("ButtonDoubleClick", typeof (object), typeof (ButtonBehaviors), 
              new UIPropertyMetadata(new PropertyChangedCallback(OnButtonDoubleClickChanged))); 

    private static void OnButtonDoubleClickChanged (DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var button = d as Button; 
     if(button == null) 
     { 
      return; 
     } 

     var command = e.NewValue as ICommand; 
     if(command == null) 
     { 
      return; 
     } 

     button.MouseDoubleClick += (o, ev) => command.Execute(button); 
    } 
} 

(我會在幾秒鐘之解釋)

然後,這裏是你將如何使用它:

MainWindow.xaml:

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication3="clr-namespace:WpfApplication3" Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Button Content="Button" 
       Height="23" 
       HorizontalAlignment="Left" 
       Margin="173,89,0,0" 
       VerticalAlignment="Top" 
       WpfApplication3:ButtonBehaviors.ButtonDoubleClick="{Binding ButtonDoubleClick}" 
       Width="75" /> 
    </Grid> 
</Window> 

最後,視圖模型:

public class ViewModel 
{ 
    private ICommand _buttonDoubeClick; 

    public ICommand ButtonDoubleClick 
    { 
     get 
     { 
      if (_buttonDoubeClick == null) 
      { 
       _buttonDoubeClick = new SimpleDelegateCommand(() => MessageBox.Show("Double click!!")); 
      } 

      return _buttonDoubeClick; 
     } 
    } 
} 

,只是爲了完整性,既然你說沒有第三方DLL的,這裏是我SimpleDelegateCommand:

public class SimpleDelegateCommand : ICommand 
{ 
    private readonly Action _action; 

    public SimpleDelegateCommand(Action action) 
    { 
     _action = action; 
    } 

    public bool CanExecute(object parameter) 
    { 
     return true; 
    } 

    public event EventHandler CanExecuteChanged; 

    public void Execute(object parameter) 
    { 
     if(_action != null) 
     { 
      _action(); 
     } 
    } 
} 

簡而言之發生的事情是這樣的:當你將對附加屬性的命令,OnButtonDoubleClickChanged事件被引發,此時我們連接到button.MouseDoubleClick事件。

+0

你的答案似乎很好,但我可以概括這個事件?或者作爲參數傳遞 – rkmax 2012-07-25 15:04:14

相關問題