LeftCardControl.xaml.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Util;
  3. using GeekDesk.ViewModel;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Collections.ObjectModel;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Navigation;
  18. using System.Windows.Shapes;
  19. namespace GeekDesk.Control.UserControls.PannelCard
  20. {
  21. /// <summary>
  22. /// LeftCardControl.xaml 的交互逻辑
  23. /// </summary>
  24. public partial class LeftCardControl : UserControl
  25. {
  26. private int menuSelectIndexTemp = -1;
  27. private AppData appData = MainWindow.appData;
  28. public LeftCardControl()
  29. {
  30. InitializeComponent();
  31. appData.AppConfig.SelectedMenuIcons = appData.MenuList[appData.AppConfig.SelectedMenuIndex].IconList;
  32. }
  33. DelegateCommand<int[]> _swap;
  34. public DelegateCommand<int[]> SwapCommand
  35. {
  36. get
  37. {
  38. if (_swap == null)
  39. _swap = new DelegateCommand<int[]>(
  40. (indexes) =>
  41. {
  42. int fromS = indexes[0];
  43. int to = indexes[1];
  44. ObservableCollection<MenuInfo> menuList = MainWindow.appData.MenuList;
  45. var elementSource = menuList[to];
  46. var dragged = menuList[fromS];
  47. menuList.Remove(dragged);
  48. menuList.Insert(to, dragged);
  49. menus.SelectedIndex = to;
  50. MainWindow.appData.MenuList = menuList;
  51. }
  52. );
  53. return _swap;
  54. }
  55. }
  56. ////菜单点击事件
  57. private void MenuClick(object sender, MouseButtonEventArgs e)
  58. {
  59. //设置对应菜单的图标列表
  60. MenuInfo mi = (MenuInfo)(((StackPanel)sender).Tag);
  61. appData.AppConfig.SelectedMenuIcons = mi.IconList;
  62. }
  63. /// <summary>
  64. /// 当修改菜单元素可见时 设置原菜单为不可见 并且不可选中
  65. /// 修改菜单元素不可见时 原菜单可见 并 选中
  66. /// </summary>
  67. /// <param name="sender"></param>
  68. /// <param name="e"></param>
  69. private void MenuWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  70. {
  71. TextBlock tb = sender as TextBlock;
  72. if (tb.Visibility == Visibility.Collapsed)
  73. {
  74. if (menus.SelectedIndex != -1)
  75. {
  76. menuSelectIndexTemp = menus.SelectedIndex;
  77. menus.SelectedIndex = -1;
  78. }
  79. else
  80. {
  81. menus.SelectedIndex = menuSelectIndexTemp;
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// 新建菜单
  87. /// </summary>
  88. /// <param name="sender"></param>
  89. /// <param name="e"></param>
  90. private void CreateMenu(object sender, RoutedEventArgs e)
  91. {
  92. MenuInfo info = new MenuInfo() { MenuEdit = Visibility.Collapsed, MenuId = System.Guid.NewGuid().ToString(), MenuName = "NewMenu" };
  93. appData.MenuList.Add(info);
  94. menus.Items.Refresh();
  95. menus.SelectedIndex = appData.MenuList.Count - 1;
  96. appData.AppConfig.SelectedMenuIndex = menus.SelectedIndex;
  97. appData.AppConfig.SelectedMenuIcons = info.IconList;
  98. }
  99. /// <summary>
  100. /// 重命名菜单 将textbox 设置为可见
  101. /// </summary>
  102. /// <param name="sender"></param>
  103. /// <param name="e"></param>
  104. private void RenameMenu(object sender, RoutedEventArgs e)
  105. {
  106. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  107. menuInfo.MenuEdit = (int)Visibility.Visible;
  108. }
  109. /// <summary>
  110. /// 删除菜单
  111. /// </summary>
  112. /// <param name="sender"></param>
  113. /// <param name="e"></param>
  114. private void DeleteMenu(object sender, RoutedEventArgs e)
  115. {
  116. MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  117. if (appData.MenuList.Count == 1)
  118. {
  119. //如果删除以后没有菜单的话 先创建一个
  120. CreateMenu(null, null);
  121. }
  122. appData.MenuList.Remove(menuInfo);
  123. if (menus.SelectedIndex == -1)
  124. {
  125. // 选中下一个菜单
  126. menus.SelectedIndex = 0;
  127. appData.AppConfig.SelectedMenuIndex = menus.SelectedIndex;
  128. appData.AppConfig.SelectedMenuIcons = appData.MenuList[0].IconList;
  129. }
  130. }
  131. /// <summary>
  132. /// 编辑菜单失焦或者敲下Enter键时保存修改后的菜单
  133. /// </summary>
  134. /// <param name="sender"></param>
  135. /// <param name="e"></param>
  136. private void LostFocusOrEnterDown(object sender, EventArgs e)
  137. {
  138. TextBox menuBox = null;
  139. if (e.GetType() == typeof(KeyEventArgs))
  140. {
  141. KeyEventArgs eKey = e as KeyEventArgs;
  142. if (eKey.Key == Key.Enter)
  143. {
  144. menuBox = ((TextBox)sender);
  145. }
  146. }
  147. else if (e.GetType() == typeof(RoutedEventArgs))
  148. {
  149. menuBox = ((TextBox)sender);
  150. }
  151. if (menuBox != null)
  152. {
  153. MenuInfo menuInfo = menuBox.Tag as MenuInfo;
  154. string text = menuBox.Text;
  155. menuInfo.MenuName = text;
  156. menuInfo.MenuEdit = Visibility.Collapsed;
  157. }
  158. }
  159. /// <summary>
  160. /// 当修改菜单元素可见时 设置全选并获得焦点
  161. /// </summary>
  162. /// <param name="sender"></param>
  163. /// <param name="e"></param>
  164. private void MenuEditWhenVisibilityChanged(object sender, DependencyPropertyChangedEventArgs e)
  165. {
  166. TextBox box = sender as TextBox;
  167. if (box.Visibility == Visibility.Visible)
  168. {
  169. Keyboard.Focus(box);
  170. box.SelectAll();
  171. }
  172. }
  173. /// <summary>
  174. /// 修改菜单图标
  175. /// </summary>
  176. /// <param name="sender"></param>
  177. /// <param name="e"></param>
  178. private void EditMenuGeometry(object sender, RoutedEventArgs e)
  179. {
  180. }
  181. }
  182. }