QuerySaleGoodsView.xaml.cs 14 KB

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