您的位置:首页 > 其它

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

2011-07-29 17:52 513 查看
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}}"/>

  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐