WPF MVVM COMMOND 传参

一、直接绑定(对于有事件的控件可以通过直接绑定的方式)

1、view

<hc:SideMenuItem Header="接谈中" Cursor="Hand" Command="{Binding AddTabItemCommand}" CommandParameter="PDjtList.xaml">
                        <hc:SideMenuItem.Icon>
                            <Image Source="/Images/icons/jtz.png" Width="24" Height="24"/>
                        </hc:SideMenuItem.Icon>
                    </hc:SideMenuItem>

2、viewmodel

/// <summary>
        /// 命令:传递参数
        /// </summary>
        public RelayCommand<string> AddTabItemCommand =>
            new Lazy<RelayCommand<string>>(() =>
                new RelayCommand<string>(AddTabItem)).Value;


        /// <summary>
        /// 添加新页面
        /// </summary>
        /// <param name="param"></param>
        private void AddTabItem(string param)
        {
            string test = param;


        }

二、EventTrigger绑定(对于通过datatemp遍历的控件没有事件可以绑定的情况,如listbox)

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
<ListBox Name="sideMenu" SelectedIndex="{Binding MenuSelectedIndex}" ItemsSource="{Binding MenuList}" BorderThickness="0" SelectionMode="Single">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <DockPanel Height="40">
                                <Image Width="20" Height="20" Source="{Binding Icons}"/>
                                <TextBlock Margin="10,12,0,0" FontSize="14" Text="{Binding Title}"/>
                                <TextBox x:Name="param" Visibility="Hidden" Text="{Binding Url}"/>
                            </DockPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                    <i:Interaction.Triggers>
                        <i:EventTrigger  EventName="SelectionChanged">
                            <command:EventToCommand Command="{Binding ElementName=sideMenu, Path=DataContext.SelectMenuCommand}" PassEventArgsToCommand="True" />
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </ListBox>
/// <summary>
        /// 菜单选择事件
        /// </summary>
        public RelayCommand<SelectionChangedEventArgs> SelectMenuCommand =>
            new Lazy<RelayCommand<SelectionChangedEventArgs>>(() =>
                new RelayCommand<SelectionChangedEventArgs>(SelectMenu)).Value;

        /// <summary>
        /// 选中菜单打开页面
        /// </summary>
        private void SelectMenu(SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count == 0) return;
            if (e.AddedItems[0] is MenuInfo item)
            {
                if (Equals(_selectedMenu, item)) return;

                _selectedMenu = item;
                DataList.Add(new TabControlModel
                {
                    Header = item.Title,
                    Url = item.Url
                });
                TabSelectedIndex = DataList.ToArray().Length - 1;

            }
        }

三、Image点击事件

<Image x:Name="img_verifycode" Height="40" Margin="0,10,30,0" Source="{Binding VerifyCode}"></Image>
                                                <i:Interaction.Triggers>
                                                    <i:EventTrigger  EventName="MouseLeftButtonDown">
                                                        <command:EventToCommand Command="{Binding ElementName=img_verifycode, Path=DataContext.SwitchVerifyCodeCommand}" />
                                                    </i:EventTrigger>
                                                </i:Interaction.Triggers>

 

 

 

 

 

 

 

 

 

 

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值