如何从FrameworkElement向事件添加ICommand?

时间:2012-07-25 14:12:44

标签: c# xaml mvvm

如何为ICommand的活动添加FrameworkElement

具体来说我想做以下

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

我想实现自己的解决方案,这是出于教育目的,我不希望使用任何第三方库(MVVM Light,Prism等)

1 个答案:

答案 0 :(得分:5)

您需要使用附加行为。出于演示目的,假设我想使用MVVM和ICommand模式实现Button Double Click,这里是相关代码:

首先,创建一个名为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>

最后,ViewModel:

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();
        }
    }
}

简而言之,发生的事情是:当您将Command分配给附加属性时,会引发OnButtonDoubleClickChanged事件,此时我们将连接到button.MouseDoubleClick事件。