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}}"/>