DepositPlanView.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Controls.Primitives;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using GalaSoft.MvvmLight.Ioc;
  16. using Muchinfo.MTPClient.Data.Enums;
  17. using Muchinfo.MTPClient.Data.Model;
  18. using Muchinfo.MTPClient.Infrastructure.Helpers;
  19. using Muchinfo.MTPClient.Infrastructure.Utilities;
  20. using Muchinfo.MTPClient.IService;
  21. using Muchinfo.MTPClient.Resources;
  22. using Muchinfo.MTPClient.Sale.ViewModels;
  23. using Muchinfo.MTPClient.Service.Utilities;
  24. using Muchinfo.PC.Common.Helpers;
  25. namespace Muchinfo.MTPClient.Sale.Views
  26. {
  27. /// <summary>
  28. /// DepositPlanView.xaml 的交互逻辑
  29. /// </summary>
  30. public partial class DepositPlanView : UserControl
  31. {
  32. public EventHandler ScrollButtonClicked;
  33. #region Private Fields
  34. private const double Const_ScrollWidth = 60d;
  35. private ScrollViewer _datagridScrollViewer; //当前DataGrid滚动条
  36. private ISystemService _systemService;
  37. #endregion
  38. #region Public Properties
  39. /// <summary>
  40. /// Gets or sets the current data grid.
  41. /// </summary>
  42. /// <value>The current data grid.</value>
  43. public WPF.Controls.DataGrid.MuchinfoDataGrid CurrentDataGrid
  44. {
  45. get;
  46. set;
  47. }
  48. /// <summary>
  49. /// 获取当前理财DataGrid的滚动条
  50. /// </summary>
  51. public ScrollViewer DataGridScrollViewer
  52. {
  53. get
  54. {
  55. if (_datagridScrollViewer != null)
  56. {
  57. return _datagridScrollViewer;
  58. }
  59. _datagridScrollViewer = WPFVisualTreeHelper.FindVisualChild<ScrollViewer>(this.DepositDataGrid);
  60. return _datagridScrollViewer;
  61. }
  62. }
  63. #endregion
  64. #region Dependency Properties
  65. /// <summary>
  66. /// 是否显示左右滚动条
  67. /// </summary>
  68. public Visibility ScrollVisibility
  69. {
  70. get { return (Visibility)GetValue(ScrollVisibilityProperty); }
  71. set { SetValue(ScrollVisibilityProperty, value); }
  72. }
  73. // Using a DependencyProperty as the backing store for ScrollVisibility. This enables animation, styling, binding, etc...
  74. public static readonly DependencyProperty ScrollVisibilityProperty = DependencyProperty.Register("ScrollVisibility", typeof(Visibility), typeof(DepositPlanView), new PropertyMetadata(Visibility.Collapsed));
  75. #endregion
  76. #region Public Methods
  77. /// <summary>
  78. /// Initializes a new instance of the <see cref=" "/> class.
  79. /// </summary>
  80. public DepositPlanView()
  81. {
  82. InitializeComponent();
  83. _systemService = SimpleIoc.Default.GetInstance<ISystemService>();
  84. this.DataContext = new DepositPlanViewModel();
  85. BuildDataGridColumns();
  86. CurrentDataGrid = this.DepositDataGrid;
  87. RegisterDataGridEvents();
  88. }
  89. /// <summary>
  90. /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
  91. /// </summary>
  92. public void Dispose()
  93. {
  94. MessengerHelper.DefaultUnregister(this);
  95. var viewModel = this.DataContext as QuerySaleGoodsViewModel;
  96. if (viewModel != null)
  97. {
  98. viewModel.Cleanup();
  99. if (_datagridScrollViewer != null)
  100. {
  101. _datagridScrollViewer.Resources.Clear();
  102. _datagridScrollViewer = null;
  103. }
  104. }
  105. this.Resources.Clear();
  106. GC.Collect();
  107. }
  108. #endregion
  109. #region Private Methods
  110. /// <summary>
  111. /// Registers the data grid events.
  112. /// </summary>
  113. private void RegisterDataGridEvents()
  114. {
  115. this.DepositDataGrid.LayoutUpdated += (s, e) =>
  116. {
  117. OnScrollButtonClicked(null);
  118. if (DataGridScrollViewer == null) return;
  119. var canScroll = DataGridScrollViewer.ScrollableWidth == 0;
  120. ScrollVisibility = canScroll ? Visibility.Collapsed : Visibility.Visible;
  121. };
  122. this.Unloaded += QuerySaleGoodsView_Unloaded;
  123. }
  124. private void BuildDataGridColumns()
  125. {
  126. var headerList = _systemService.GetQuoteListHeaders(eTradeMode.Deposit);
  127. var quoteListHeaders = (headerList == null) ? null : headerList.ToList();
  128. if (quoteListHeaders == null || !quoteListHeaders.Any()) return;
  129. ////根据QuoteListHeader集合创建行情DataGrid列集合
  130. var columns = QuoteDataGridHelper.CreateQuoteDataGridColumns(quoteListHeaders);
  131. if (columns == null || !columns.Any()) return;
  132. ////清除所有列
  133. DepositDataGrid.Columns.Clear();
  134. ////添加列
  135. foreach (var column in columns)
  136. {
  137. DepositDataGrid.Columns.Add(column);
  138. }
  139. ////添加操作按钮列
  140. var operationStackPanel = new StackPanel()
  141. {
  142. HorizontalAlignment = HorizontalAlignment.Stretch
  143. };
  144. operationStackPanel.Children.Add(new TextBlock() { Text = Client_Resource.Content_Operation, HorizontalAlignment = HorizontalAlignment.Center });
  145. var operationDataTemplate = this.Resources["OperationDataTemplate"] as DataTemplate;
  146. var operationColumn = new QuoteDataGridTemplateColumn()
  147. {
  148. ////QuoteListHeader设为空,不保存此列
  149. QuoteListHeader = null,
  150. Header = operationStackPanel,
  151. Visibility = Visibility.Visible,
  152. CellTemplate = operationDataTemplate,
  153. Width = new DataGridLength(1, DataGridLengthUnitType.Auto),
  154. };
  155. DepositDataGrid.Columns.Add(operationColumn);
  156. }
  157. /// <summary>
  158. /// 滚动按钮滚动时
  159. /// </summary>
  160. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  161. private void OnScrollButtonClicked(EventArgs e)
  162. {
  163. if (ScrollButtonClicked != null)
  164. {
  165. ScrollButtonClicked(null, e);
  166. }
  167. }
  168. #region Route Events
  169. /// <summary>
  170. /// Handles the Unloaded event of the QuerySaleGoodsView control.
  171. /// </summary>
  172. /// <param name="sender">The source of the event.</param>
  173. /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
  174. private void QuerySaleGoodsView_Unloaded(object sender, RoutedEventArgs e)
  175. {
  176. // Dispose();
  177. }
  178. /// <summary>
  179. /// Handles the OnKeyDown event of the SaleDataGrid control.
  180. /// </summary>
  181. /// <param name="sender">The source of the event.</param>
  182. /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
  183. private void DepositDataGrid_OnKeyDown(object sender, KeyEventArgs e)
  184. {
  185. if (e.Key == Key.Left)
  186. {
  187. if (DataGridScrollViewer == null) return;
  188. DataGridScrollViewer.ScrollToHorizontalOffset(DataGridScrollViewer.HorizontalOffset - Const_ScrollWidth);
  189. e.Handled = true;
  190. }
  191. else if (e.Key == Key.Right)
  192. {
  193. if (DataGridScrollViewer == null) return;
  194. DataGridScrollViewer.ScrollToHorizontalOffset(DataGridScrollViewer.HorizontalOffset + Const_ScrollWidth);
  195. e.Handled = true;
  196. }
  197. }
  198. /// <summary>
  199. /// Handles the OnPreviewMouseWheel event of the SaleDataGrid control.
  200. /// </summary>
  201. /// <param name="sender">The source of the event.</param>
  202. /// <param name="e">The <see cref="MouseWheelEventArgs"/> instance containing the event data.</param>
  203. private void DepositDataGrid_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
  204. {
  205. if (e.Delta > 0)
  206. {
  207. DataGridScrollViewer.PageUp();
  208. }
  209. if (e.Delta < 0)
  210. {
  211. DataGridScrollViewer.PageDown();
  212. }
  213. e.Handled = true;
  214. }
  215. /// <summary>
  216. /// Handles the OnMouseRightButtonDown event of the SaleDataGrid control.
  217. /// </summary>
  218. /// <param name="sender">The source of the event.</param>
  219. /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
  220. private void DepositDataGrid_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
  221. {
  222. var obj = e.OriginalSource as DependencyObject;
  223. while (obj != null && !(obj is DataGridColumnHeader))
  224. {
  225. obj = VisualTreeHelper.GetParent(obj);
  226. }
  227. var header = obj as DataGridColumnHeader;
  228. if (header != null)
  229. {
  230. ////注释掉表头左键菜单,使用统一菜单
  231. ////header.ContextMenu = _headerContextMenu;
  232. //var item = QuoteListDataGrid.SelectedItem as QuoteGoodsDTO;
  233. //if (item == null) return;
  234. //BuildContextMenu(item);
  235. }
  236. else
  237. {
  238. var rowContextMenu = new ContextMenu();
  239. var menuItemStyle = ResourceHelper.GetFromeResource<Style>("ContextCommonMenuItem");
  240. var row = e.OriginalSource as DependencyObject;
  241. while (row != null && !(row is DataGridRow))
  242. {
  243. row = VisualTreeHelper.GetParent(row);
  244. }
  245. var dataRow = row as DataGridRow;
  246. if (dataRow != null)
  247. {
  248. var menuItemApply = new MenuItem()
  249. {
  250. Header = Client_Resource.Resources_Service_DepositOrder,
  251. Tag = ContextMenuCommandType.DepositApply,
  252. // DataContext = menuItemApply,
  253. //Margin = new Thickness(0, 3, 0, 3),
  254. Style = menuItemStyle,
  255. };
  256. menuItemApply.Click += RefreshGoodsList_Click;
  257. var plane = dataRow.DataContext as DepositPlan;
  258. if (plane != null)
  259. {
  260. menuItemApply.IsEnabled = UserManager.IsAccountLogin && plane.IsDepositApply;
  261. }
  262. rowContextMenu.Items.Add(menuItemApply);
  263. }
  264. var menuItem = new MenuItem()
  265. {
  266. Header = ContextMenuNames.RefreshGoodsList,
  267. Tag = ContextMenuCommandType.RefreshGoodsList,
  268. //DataContext = item,
  269. Style = menuItemStyle,
  270. };
  271. menuItem.Click += RefreshGoodsList_Click;
  272. menuItem.IsEnabled = UserManager.IsAccountLogin; ////只有账号登录了才有效
  273. rowContextMenu.Items.Add(menuItem);
  274. this.DepositDataGrid.ContextMenu = rowContextMenu;
  275. //}
  276. //else
  277. //{
  278. // ////在DataGrid外点击右键,取消右键菜单
  279. // this.SaleDataGrid.ContextMenu = null;
  280. //}
  281. }
  282. }
  283. /// <summary>
  284. /// Handles the Click event of the RefreshGoodsList control.
  285. /// </summary>
  286. /// <param name="sender">The source of the event.</param>
  287. /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
  288. private void RefreshGoodsList_Click(object sender, RoutedEventArgs e)
  289. {
  290. var menuItem = sender as MenuItem;
  291. if (menuItem == null) return;
  292. var commandType = (ContextMenuCommandType)menuItem.Tag;
  293. var viewModel = this.DataContext as DepositPlanViewModel;
  294. switch (commandType)
  295. {
  296. case ContextMenuCommandType.RefreshGoodsList:
  297. ////刷新商品列表
  298. if (viewModel != null)
  299. {
  300. viewModel.SelectCommand.Execute(null);
  301. }
  302. break;
  303. case ContextMenuCommandType.DepositApply:
  304. if (viewModel != null)
  305. {
  306. viewModel.DetailCommand.Execute(null);
  307. }
  308. break;
  309. }
  310. }
  311. #endregion
  312. #endregion
  313. }
  314. }