WPF MVVM COMMOND 传参

2021-01-19 05:11

阅读:349

标签:RoCE   hang   control   打开   nta   dock   ram   pre   tar   

原文:WPF MVVM COMMOND 传参

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

1、view

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

2、viewmodel

  1. ///
  2. /// 命令:传递参数
  3. ///
  4. public RelayCommandstring> AddTabItemCommand =>
  5. new Lazystring>>(() =>
  6. new RelayCommandstring>(AddTabItem)).Value;
  7. ///
  8. /// 添加新页面
  9. ///
  10. ///
  11. private void AddTabItem(string param)
  12. {
  13. string test = param;
  14. }

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

技术图片

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
  1. "sideMenu" SelectedIndex="{Binding MenuSelectedIndex}" ItemsSource="{Binding MenuList}" BorderThickness="0" SelectionMode="Single">
  2. "40">
  3. "20" Height="20" Source="{Binding Icons}"/>
  4. "10,12,0,0" FontSize="14" Text="{Binding Title}"/>
  5. "param" Visibility="Hidden" Text="{Binding Url}"/>
  6. "SelectionChanged">
  7. "{Binding ElementName=sideMenu, Path=DataContext.SelectMenuCommand}" PassEventArgsToCommand="True" />
  1. ///
  2. /// 菜单选择事件
  3. ///
  4. public RelayCommand SelectMenuCommand =>
  5. new Lazy>(() =>
  6. new RelayCommand(SelectMenu)).Value;
  7. ///
  8. /// 选中菜单打开页面
  9. ///
  10. private void SelectMenu(SelectionChangedEventArgs e)
  11. {
  12. if (e.AddedItems.Count == 0) return;
  13. if (e.AddedItems[0] is MenuInfo item)
  14. {
  15. if (Equals(_selectedMenu, item)) return;
  16. _selectedMenu = item;
  17. DataList.Add(new TabControlModel
  18. {
  19. Header = item.Title,
  20. Url = item.Url
  21. });
  22. TabSelectedIndex = DataList.ToArray().Length - 1;
  23. }
  24. }

三、Image点击事件

  1. "img_verifycode" Height="40" Margin="0,10,30,0" Source="{Binding VerifyCode}">
  2. "MouseLeftButtonDown">
  3. "{Binding ElementName=img_verifycode, Path=DataContext.SwitchVerifyCodeCommand}" />

 

 

 

 

 

 

 

 

 

 

 

 

WPF MVVM COMMOND 传参

标签:RoCE   hang   control   打开   nta   dock   ram   pre   tar   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12164917.html


评论


亲,登录后才可以留言!