8,737
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 扩展CommandParameter,使CommandParameter可以带事件参数
/// </summary>
public class ExCommandParameter
{
/// <summary>
/// 事件触发源
/// </summary>
public DependencyObject Sender { get; set; }
/// <summary>
/// 事件参数
/// </summary>
public EventArgs EventArgs { get; set; }
/// <summary>
/// 额外参数
/// </summary>
public object Parameter { get; set; }
}
/// <summary>
/// 扩展的InvokeCommandAction
/// </summary>
public class ExInvokeCommandAction : TriggerAction<DependencyObject>
{
private string commandName;
public static readonly DependencyProperty CommandProperty = DependencyProperty.Register("Command", typeof(ICommand), typeof(ExInvokeCommandAction), null);
public static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExInvokeCommandAction), null);
/// <summary>
/// 获得或设置此操作应调用的命令的名称。
/// </summary>
/// <value>此操作应调用的命令的名称。</value>
/// <remarks>如果设置了此属性和 Command 属性,则此属性将被后者所取代。</remarks>
public string CommandName
{
get
{
base.ReadPreamble();
return this.commandName;
}
set
{
if (this.CommandName != value)
{
base.WritePreamble();
this.commandName = value;
base.WritePostscript();
}
}
}
/// <summary>
/// 获取或设置此操作应调用的命令。这是依赖属性。
/// </summary>
/// <value>要执行的命令。</value>
/// <remarks>如果设置了此属性和 CommandName 属性,则此属性将优先于后者。</remarks>
public ICommand Command
{
get
{
return (ICommand)base.GetValue(ExInvokeCommandAction.CommandProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandProperty, value);
}
}
/// <summary>
/// 获得或设置命令参数。这是依赖属性。
/// </summary>
/// <value>命令参数。</value>
/// <remarks>这是传递给 ICommand.CanExecute 和 ICommand.Execute 的值。</remarks>
public object CommandParameter
{
get
{
return base.GetValue(ExInvokeCommandAction.CommandParameterProperty);
}
set
{
base.SetValue(ExInvokeCommandAction.CommandParameterProperty, value);
}
}
/// <summary>
/// 调用操作。
/// </summary>
/// <param name="parameter">操作的参数。如果操作不需要参数,则可以将参数设置为空引用。</param>
protected override void Invoke(object parameter)
{
if (base.AssociatedObject != null)
{
ICommand command = this.ResolveCommand();
ExCommandParameter exParameter = new ExCommandParameter
{
Sender = base.AssociatedObject,
Parameter = GetValue(CommandParameterProperty),
EventArgs = parameter as EventArgs
};
if (command != null && command.CanExecute(exParameter))
{
command.Execute(exParameter);
}
}
}
private ICommand ResolveCommand()
{
ICommand result = null;
if (this.Command != null)
{
result = this.Command;
}
else
{
if (base.AssociatedObject != null)
{
Type type = base.AssociatedObject.GetType();
PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
PropertyInfo[] array = properties;
for (int i = 0; i < array.Length; i++)
{
PropertyInfo propertyInfo = array[i];
if (typeof(ICommand).IsAssignableFrom(propertyInfo.PropertyType) && string.Equals(propertyInfo.Name, this.CommandName, StringComparison.Ordinal))
{
result = (ICommand)propertyInfo.GetValue(base.AssociatedObject, null);
}
}
}
}
return result;
}
}
xaml里面这样:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<loc:ExInvokeCommandAction Command="{Binding SelectionChangeCmd}" CommandParameter="{Binding ElementName=tabControlName}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
loc为扩展类ExInvokeCommandAction 的命名空间
viewmodel里这样判断
private void SelectionChangeCmdExecuted(object parameter)
{
if (parameter is ExCommandParameter)
{
ExCommandParameter ExParameter = parameter as ExCommandParameter;
if (ExParameter.EventArgs != EventArgs.Empty && ExParameter.EventArgs is SelectionChangedEventArgs)
{
SelectionChangedEventArgs args = ExParameter.EventArgs as SelectionChangedEventArgs;
if (args.AddedItems.Count != 0 && args.AddedItems[0] is TabItem)
{
if (ExParameter.Parameter != null && ExParameter.Parameter is System.Windows.Controls.TabControl)
{
System.Windows.Controls.TabControl item = ExParameter.Parameter as System.Windows.Controls.TabControl;
switch (item.SelectedIndex)
{
case 0:
FtRLoaded();
break;
case 1:
RtFLoaded();
break;
case 2:
FtSLoaded();
break;
case 3:
TtGLoaded();
break;
}
}
}
}
}
}