MainWindow.xaml.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using System.Windows.Input;
  5. using System.Windows.Media.Imaging;
  6. using GeekDesk.ViewModel;
  7. using System.IO;
  8. using GeekDesk.Util;
  9. using GalaSoft.MvvmLight;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. using System.Collections.ObjectModel;
  13. using WPF.JoshSmith.ServiceProviders.UI;
  14. using DraggAnimatedPanelExample;
  15. using System.ComponentModel;
  16. namespace GeekDesk
  17. {
  18. /// <summary>
  19. /// MainWindow.xaml 的交互逻辑
  20. /// </summary>
  21. ///
  22. public partial class MainWindow : Window
  23. {
  24. private static MainModel mainModel;
  25. ListViewDragDropManager<ViewModel.Menu> dragMgr;
  26. ListViewDragDropManager<ViewModel.DataInfos> dragMgr2;
  27. public MainWindow()
  28. {
  29. InitializeComponent();
  30. mainModel = new MainModel();
  31. //this.DataContext = mainModel;
  32. //menu.Items = mainModel;
  33. //System.Diagnostics.Process.Start(@"D:\SoftWare\WeGame\wegame.exe");
  34. this.Loaded += Window_Loaded;
  35. this.SizeChanged += MainWindow_Resize;
  36. }
  37. DelegateCommand<int[]> _swap;
  38. public DelegateCommand<int[]> SwapCommand
  39. {
  40. get
  41. {
  42. if (_swap == null)
  43. _swap = new DelegateCommand<int[]>(
  44. (indexes) =>
  45. {
  46. int fromS = indexes[0];
  47. int to = indexes[1];
  48. var elementSource = data.Items[to];
  49. var dragged = data.Items[fromS];
  50. if (fromS > to)
  51. {
  52. data.Items.Remove(dragged);
  53. data.Items.Insert(to, dragged);
  54. }
  55. else
  56. {
  57. data.Items.Remove(dragged);
  58. data.Items.Insert(to, dragged);
  59. }
  60. }
  61. );
  62. return _swap;
  63. }
  64. }
  65. DelegateCommand<int[]> _swap2;
  66. public DelegateCommand<int[]> SwapCommand2
  67. {
  68. get
  69. {
  70. if (_swap2 == null)
  71. _swap2 = new DelegateCommand<int[]>(
  72. (indexes) =>
  73. {
  74. int fromS = indexes[0];
  75. int to = indexes[1];
  76. var elementSource = menu.Items[to];
  77. var dragged = menu.Items[fromS];
  78. if (fromS > to)
  79. {
  80. menu.Items.Remove(dragged);
  81. menu.Items.Insert(to, dragged);
  82. }
  83. else
  84. {
  85. menu.Items.Remove(dragged);
  86. menu.Items.Insert(to, dragged);
  87. }
  88. }
  89. );
  90. return _swap2;
  91. }
  92. }
  93. private void Wrap_Drop(object sender, DragEventArgs e)
  94. {
  95. Array dropObject = (System.Array)e.Data.GetData(DataFormats.FileDrop);
  96. if (dropObject == null) return;
  97. string path = (string)dropObject.GetValue(0);
  98. if (File.Exists(path))
  99. {
  100. // 文件
  101. BitmapImage bi = FileIcon.GetBitmapImage(path);
  102. DataInfos infos = new DataInfos();
  103. infos.Path = path;
  104. infos.BitmapImage = bi;
  105. infos.Name = Path.GetFileNameWithoutExtension(path);
  106. data.Items.Add(infos);
  107. data.Items.Refresh();
  108. }
  109. else if (Directory.Exists(path))
  110. {
  111. //文件夹
  112. }
  113. }
  114. //菜单点击事件
  115. private void menuClick(object sender, MouseButtonEventArgs e)
  116. {
  117. }
  118. /// <summary>
  119. /// 图标点击事件
  120. /// </summary>
  121. /// <param name="sender"></param>
  122. /// <param name="e"></param>
  123. private void dataClick(object sender, MouseButtonEventArgs e)
  124. {
  125. //string path = ((StackPanel)sender).Tag.ToString();
  126. //System.Diagnostics.Process.Start(path);
  127. }
  128. /// <summary>
  129. /// data选中事件 设置不可选中
  130. /// </summary>
  131. /// <param name="sender"></param>
  132. /// <param name="e"></param>
  133. private void data_SelectionChanged(object sender, SelectionChangedEventArgs e)
  134. {
  135. if (data.SelectedIndex != -1) data.SelectedIndex = -1;
  136. }
  137. #region Window_Loaded
  138. void Window_Loaded(object sender, RoutedEventArgs e)
  139. {
  140. AppConfig config = CommonCode.GetAppConfig();
  141. this.Width = config.WindowWidth;
  142. this.Height = config.WindowHeight;
  143. this.DataContext = config;
  144. this.menu.Items.Add(new ViewModel.Menu() { menu = "test1" });
  145. this.menu.Items.Add(new ViewModel.Menu() { menu = "test2" });
  146. this.menu.Items.Add(new ViewModel.Menu() { menu = "test3" });
  147. }
  148. #endregion // Window_Loaded
  149. #region Window_Closing
  150. void Window_Closing(object sender, CancelEventArgs e)
  151. {
  152. Rect rect = this.RestoreBounds;
  153. AppConfig config = this.DataContext as AppConfig;
  154. config.WindowWidth = rect.Width;
  155. config.WindowHeight = rect.Height;
  156. CommonCode.SaveAppConfig(config);
  157. }
  158. #endregion // Window_Closing
  159. void MainWindow_Resize(object sender, System.EventArgs e)
  160. {
  161. if (this.DataContext != null)
  162. {
  163. AppConfig config = this.DataContext as AppConfig;
  164. config.WindowWidth = this.Width;
  165. config.WindowHeight = this.Height;
  166. CommonCode.SaveAppConfig(config);
  167. }
  168. }
  169. #region dragMgr_ProcessDrop
  170. // Performs custom drop logic for the top ListView.
  171. void dragMgr_ProcessDrop(object sender, ProcessDropEventArgs<object> e)
  172. {
  173. // This shows how to customize the behavior of a drop.
  174. // Here we perform a swap, instead of just moving the dropped item.
  175. int higherIdx = Math.Max(e.OldIndex, e.NewIndex);
  176. int lowerIdx = Math.Min(e.OldIndex, e.NewIndex);
  177. if (lowerIdx < 0)
  178. {
  179. // The item came from the lower ListView
  180. // so just insert it.
  181. e.ItemsSource.Insert(higherIdx, e.DataItem);
  182. }
  183. else
  184. {
  185. // null values will cause an error when calling Move.
  186. // It looks like a bug in ObservableCollection to me.
  187. if (e.ItemsSource[lowerIdx] == null ||
  188. e.ItemsSource[higherIdx] == null)
  189. return;
  190. // The item came from the ListView into which
  191. // it was dropped, so swap it with the item
  192. // at the target index.
  193. e.ItemsSource.Move(lowerIdx, higherIdx);
  194. e.ItemsSource.Move(higherIdx - 1, lowerIdx);
  195. }
  196. // Set this to 'Move' so that the OnListViewDrop knows to
  197. // remove the item from the other ListView.
  198. e.Effects = DragDropEffects.Move;
  199. }
  200. #endregion // dragMgr_ProcessDrop
  201. #region OnListViewDragEnter
  202. // Handles the DragEnter event for both ListViews.
  203. void OnListViewDragEnter(object sender, DragEventArgs e)
  204. {
  205. e.Effects = DragDropEffects.Move;
  206. }
  207. #endregion // OnListViewDragEnter
  208. #region OnListViewDrop
  209. // Handles the Drop event for both ListViews.
  210. void OnListViewDrop(object sender, DragEventArgs e)
  211. {
  212. if (e.Effects == DragDropEffects.None)
  213. return;
  214. ViewModel.Menu menuV = e.Data.GetData(typeof(ViewModel.Menu)) as ViewModel.Menu;
  215. DataInfos data = e.Data.GetData(typeof(DataInfos)) as DataInfos;
  216. if (sender == this.menu)
  217. {
  218. if (this.dragMgr.IsDragInProgress)
  219. return;
  220. // An item was dragged from the bottom ListView into the top ListView
  221. // so remove that item from the bottom ListView.
  222. (this.data.ItemsSource as ObservableCollection<DataInfos>).Remove(data);
  223. }
  224. else
  225. {
  226. if (this.dragMgr2.IsDragInProgress)
  227. return;
  228. // An item was dragged from the top ListView into the bottom ListView
  229. // so remove that item from the top ListView.
  230. (this.menu.ItemsSource as ObservableCollection<ViewModel.Menu>).Remove(menuV);
  231. }
  232. }
  233. #endregion // OnListViewDrop
  234. private void leftCard_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
  235. {
  236. }
  237. private void deleteMenu(object sender, RoutedEventArgs e)
  238. {
  239. //if (data.SelectedIndex == -1)
  240. //{
  241. // return;
  242. //}
  243. ViewModel.Menu pojo = (ViewModel.Menu)((ContextMenu)((MenuItem)sender).Parent).DataContext;
  244. string menuTitle = pojo.menu;
  245. int index = 0;
  246. foreach (object obj in menu.Items)
  247. {
  248. string test = ((ViewModel.Menu)obj).menu;
  249. if (test == menuTitle)
  250. {
  251. menu.Items.RemoveAt(index);
  252. menu.Items.Refresh();
  253. return;
  254. }
  255. index++;
  256. }
  257. }
  258. public Double ConvertString(string val)
  259. {
  260. return Convert.ToDouble(val);
  261. }
  262. }
  263. public class MainModel : ViewModelBase
  264. {
  265. public List<ViewModel.Menu> MenuList { get; set; }
  266. public List<ViewModel.DataInfos> DataList { get; set; }
  267. }
  268. }