MVVM 的事件处理: 利用Prism框架进行EventToCommand

Prism框架只对Button提供了Command的附加属性,虽然通过ControlTemplate可以实现大部分的功能,但是总是重写ControlTemplate未免费时费力,而且重写的ControlTemplate还不一定有原来的动态效果,这里提供一个解决方案。

此处对ComboBox的SelectionChanged事件做EventToCommand。

这里用到Prism中的CommandBehaviorBase类。

1. 定义附加属性类SelectedChanged.cs

 public class SelectedChanged
    {
        private static readonly DependencyProperty SelectedChangedCommandBehaviorProperty = DependencyProperty.RegisterAttached(
          "SelectedChangedCommandBehavior",
          typeof(ComboBoxSelectChangedCommandBehavior),
          typeof(SelectedChanged),
          null);

        public static readonly DependencyProperty CommandProperty = DependencyProperty.RegisterAttached(
            "Command",
            typeof(ICommand),
            typeof(SelectedChanged),
            new PropertyMetadata(OnSetCommandCallback));

        public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.RegisterAttached(
            "CommandParameter",
            typeof(object),
            typeof(SelectedChanged),
            new PropertyMetadata(OnSetCommandParameterCallback));


        public static void SetCommand(ComboBox CBox, ICommand command)
        {
            if (CBox == null) throw new System.ArgumentNullException("CBox");
            CBox.SetValue(CommandProperty, command);
        }

        public static ICommand GetCommand(ComboBox CBox)
        {
            if (CBox == null) throw new System.ArgumentNullException("CBox");
            return CBox.GetValue(CommandProperty) as ICommand;
        }

        public static void SetCommandParameter(ComboBox CBox, object parameter)
        {
            if (CBox == null) throw new System.ArgumentNullException("CBox");
            CBox.SetValue(CommandParameterProperty, parameter);
        }

        public static object GetCommandParameter(ComboBox CBox)
        {
            if (CBox == null) throw new System.ArgumentNullException("CBox");
            return CBox.GetValue(CommandParameterProperty);
        }

        private static void OnSetCommandCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            ComboBox CBox = dependencyObject as ComboBox;
            if (CBox != null)
            {
                ComboBoxSelectChangedCommandBehavior behavior = GetOrCreateBehavior(CBox);
                behavior.Command = e.NewValue as ICommand;
            }
        }

        private static void OnSetCommandParameterCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            ComboBox CBox = dependencyObject as ComboBox;
            if (CBox != null)
            {
                ComboBoxSelectChangedCommandBehavior behavior = GetOrCreateBehavior(CBox);
                behavior.CommandParameter = e.NewValue;
            }
        }

        private static ComboBoxSelectChangedCommandBehavior GetOrCreateBehavior(ComboBox CBox)
        {
            ComboBoxSelectChangedCommandBehavior behavior = CBox.GetValue(SelectedChangedCommandBehaviorProperty) as ComboBoxSelectChangedCommandBehavior;
            if (behavior == null)
            {
                behavior = new ComboBoxSelectChangedCommandBehavior(CBox);
                CBox.SetValue(SelectedChangedCommandBehaviorProperty, behavior);
            }

            return behavior;
        }
    }

2. 定义Behavior类

  public ComboBoxSelectChangedCommandBehavior(ComboBox CMBObject)
            : base(CMBObject)
        {
            if (CMBObject == null) throw new System.ArgumentNullException("CMBObject can't be null");
            CMBObject.SelectionChanged += (s, e) =>
            {
                ExecuteCommand();
            };
        }

 OK!接下来在ComboBox就可以使用这个附加属性了

 Lcommands:SelectedChanged.Command="{Binding FamilySelectCommand}"  
 Lcommands:SelectedChanged.CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource Self}}"/>

  

 

转载于:https://www.cnblogs.com/v-jing/archive/2011/07/29/2121315.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值