【问题标题】:How can I add a ICommand to an event from FrameworkElement?如何将 ICommand 添加到来自 FrameworkElement 的事件?
【发布时间】:2012-07-25 14:12:43
【问题描述】:

如何将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 等)

【问题讨论】:

    标签: c# xaml mvvm


    【解决方案1】:

    您需要使用附加行为。出于演示目的,假设我想使用 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 事件。

    【讨论】:

    • 你的回答似乎很好,但我可以概括一下这个事件吗?或作为参数传递
    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 2012-06-11
    • 2012-06-11
    • 2021-05-21
    • 1970-01-01
    • 2014-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多