如何在mvvm下停止TabControl selectionChanged命令向下路由

zhz5214 2012-12-23 11:45:05
在使用TabControl时为延缓TabItem初始化使用了下述code:
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectionChangeCmd}" CommandParameter="{Binding SelectedIndex,ElementName=tabControlName}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
结果是tabitem里面的combobox SelectionChanged也触发命令。
怎样做才能只让tabcontrol响应事件?

...全文
151 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhz5214 2012-12-23
  • 打赏
  • 举报
回复
自己解决了,扩展了下InvokeCommandAction,将EventArgs传进去,在viewModel进行判断,如果不是的tabItem就不响应命令。
/// <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; 
                            }
                        } 
                    } 
                }
            } 
        }

8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧