QuoteBoardView.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. using System.Windows.Media.Animation;
  2. using Muchinfo.MTPClient.Data.DTO;
  3. using Muchinfo.MTPClient.Data.Enums;
  4. using Muchinfo.MTPClient.Data.Model;
  5. using Muchinfo.MTPClient.Infrastructure.Helpers;
  6. using Muchinfo.MTPClient.Infrastructure.Interfaces;
  7. using Muchinfo.MTPClient.Quotation.ViewModels;
  8. using Muchinfo.PC.Common.Helpers;
  9. using System;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Input;
  13. using Muchinfo.MTPClient.Infrastructure.Cache;
  14. using System.Linq;
  15. using Muchinfo.MTPClient.IService;
  16. using GalaSoft.MvvmLight.Ioc;
  17. using Muchinfo.MTPClient.Infrastructure.Utilities;
  18. namespace Muchinfo.MTPClient.Quotation.Views
  19. {
  20. /// <summary>
  21. /// QuoteBoardView.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class QuoteBoardView : UserControl, IDisposable, IQuoteView
  24. {
  25. #region Fields
  26. private const double Const_ScrollWidth = 60d;
  27. private MenuCommandType _currentType;
  28. private string _additionalInfo;
  29. private MarketsInfoModel _goodsGroup;
  30. private ISystemService _systemService;
  31. private ScrollViewer _datagridScrollViewer; //当前行情DataGrid滚动条
  32. #endregion
  33. #region Properties
  34. private QuoteBoardViewModel ViewModel
  35. {
  36. get
  37. {
  38. return this.DataContext as QuoteBoardViewModel;
  39. }
  40. }
  41. public ScrollViewer DataGridScrollViewer
  42. {
  43. get
  44. {
  45. if (_datagridScrollViewer != null)
  46. {
  47. return _datagridScrollViewer;
  48. }
  49. _datagridScrollViewer = WPFVisualTreeHelper.FindVisualChild<ScrollViewer>(this.QuoteListBox);
  50. return _datagridScrollViewer;
  51. }
  52. }
  53. #endregion
  54. #region Constructors
  55. public QuoteBoardView(MenuCommandType commandType, MarketsInfoModel additionalInfo)
  56. {
  57. InitializeComponent();
  58. _systemService = SimpleIoc.Default.GetInstance<ISystemService>();
  59. this.DataContext = new QuoteBoardViewModel();
  60. RefreshList(commandType, additionalInfo);
  61. _goodsGroup = additionalInfo;
  62. this.QuoteListBox.PreviewMouseWheel += QuoteListBox_PreviewMouseWheel;
  63. this.QuoteListBox.SelectionChanged += QuoteListBox_SelectionChanged;
  64. this.QuoteListBox.KeyUp += QuoteListBox_KeyUp;
  65. MessengerHelper.DefaultRegister<QuoteGoods>(this, MessengerTokens.QuoteBoardSelectItemChange, (goods) =>
  66. {
  67. if (QuoteListBox == null) return;
  68. QuoteListBox.SelectedItem = goods;
  69. });
  70. MessengerHelper.DefaultRegister<string>(this, MessengerTokens.RefreshGoodsToken, (msg) =>
  71. {
  72. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  73. {
  74. RefreshList(_currentType, _goodsGroup);
  75. }));
  76. });
  77. this.Unloaded += QuoteBoardView_Unloaded;
  78. if (!IsDeliveryVisible)
  79. {
  80. this.PAndXGoodsInfoDataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
  81. }
  82. }
  83. /// <summary>
  84. /// 删除注册内容
  85. /// </summary>
  86. /// <param name="sender"></param>
  87. /// <param name="e"></param>
  88. void QuoteBoardView_Unloaded(object sender, RoutedEventArgs e)
  89. {
  90. //MessengerHelper.DefaultUnregister(MessengerTokens.RefreshGoodsToken);
  91. //MessengerHelper.DefaultUnregister(MessengerTokens.RegisterResourceChange);
  92. //MessengerHelper.DefaultUnregister(MessengerTokens.QuoteBoardSelectItemChange);
  93. //Dispose();
  94. }
  95. #endregion
  96. #region Event Handlers
  97. /// <summary>
  98. /// Handles the KeyUp event of the QuoteListBox control.
  99. /// </summary>
  100. /// <param name="sender">The source of the event.</param>
  101. /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
  102. private void QuoteListBox_KeyUp(object sender, KeyEventArgs e)
  103. {
  104. if (e.Key == Key.Left)
  105. {
  106. if (DataGridScrollViewer == null) return;
  107. DataGridScrollViewer.ScrollToHorizontalOffset(DataGridScrollViewer.HorizontalOffset - Const_ScrollWidth);
  108. e.Handled = true;
  109. }
  110. else if (e.Key == Key.Right)
  111. {
  112. if (DataGridScrollViewer == null) return;
  113. DataGridScrollViewer.ScrollToHorizontalOffset(DataGridScrollViewer.HorizontalOffset + Const_ScrollWidth);
  114. e.Handled = true;
  115. }
  116. }
  117. /// <summary>
  118. /// Handles the SelectionChanged event of the QuoteListBox control.
  119. /// </summary>
  120. /// <param name="sender">The source of the event.</param>
  121. /// <param name="e">The <see cref="SelectionChangedEventArgs"/> instance containing the event data.</param>
  122. private void QuoteListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
  123. {
  124. if (e.AddedItems != null && e.AddedItems.Count > 0)
  125. {
  126. var quoteGoods = e.AddedItems[0] as QuoteGoodsDTO;
  127. if (quoteGoods != null)
  128. {
  129. MessengerHelper.DefaultSend<QuoteGoodsDTO>(quoteGoods, MessengerTokens.SelectGoodsChange);
  130. if (ViewModel != null)
  131. {
  132. ViewModel.SelectGoodsOfPAndX_ItemSource(quoteGoods);
  133. if (ViewModel.GoodsOfPandXInfoList != null)
  134. {
  135. var fistGoods = ViewModel.GoodsOfPandXInfoList.FirstOrDefault();
  136. if (fistGoods != null)
  137. {
  138. var group = CacheManager.CacheMarketsGroups.FirstOrDefault(
  139. (item) => item.MarketID == fistGoods.GoodsGroupId);
  140. BuildDeliveryDataGridColumns(group);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. #region 交割行情列表
  148. private void BuildDeliveryDataGridColumns(MarketsInfoModel goosGroup)
  149. {
  150. var type = GetQuoteListHeaderTypeByeMarket(goosGroup);
  151. var headerList = _systemService.GetDeliveryQuoteListHeaders(type);
  152. var quoteListHeaders = (headerList == null) ? null : headerList.ToList();
  153. if (quoteListHeaders == null || !quoteListHeaders.Any()) return;
  154. ////根据QuoteListHeader集合创建行情DataGrid列集合
  155. var columns = QuoteDataGridHelper.CreateQuoteDataGridColumns(quoteListHeaders);
  156. if (columns == null || !columns.Any()) return;
  157. ////清除所有列
  158. PAndXGoodsInfoDataGrid.Columns.Clear();
  159. ////添加列
  160. foreach (var column in columns)
  161. {
  162. PAndXGoodsInfoDataGrid.Columns.Add(column);
  163. }
  164. }
  165. #endregion
  166. /// <summary>
  167. /// 根据市场获取列头
  168. /// </summary>
  169. /// <param name="goodsGroup">The goods group.</param>
  170. /// <returns>MarketType.</returns>
  171. private eTradeMode GetQuoteListHeaderTypeByeMarket(MarketsInfoModel goodsGroup)
  172. {
  173. if (goodsGroup == null)
  174. {
  175. return eTradeMode.TRADEMODE_MARKETMAKE;
  176. }
  177. return goodsGroup.TradeMode;
  178. }
  179. /// <summary>
  180. /// Handles the PreviewMouseWheel event of the QuoteListBox control.
  181. /// </summary>
  182. /// <param name="sender">The source of the event.</param>
  183. /// <param name="e">The <see cref="MouseWheelEventArgs"/> instance containing the event data.</param>
  184. private void QuoteListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
  185. {
  186. if (e.Delta > 0)
  187. {
  188. DataGridScrollViewer.PageUp();
  189. }
  190. if (e.Delta < 0)
  191. {
  192. DataGridScrollViewer.PageDown();
  193. }
  194. e.Handled = true;
  195. }
  196. #endregion
  197. #region IDispose
  198. /// <summary>
  199. /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
  200. /// </summary>
  201. public void Dispose()
  202. {
  203. MessengerHelper.DefaultUnregister(this);
  204. if (ViewModel != null) ViewModel.Cleanup();
  205. if (QuoteListBox != null)
  206. {
  207. this.QuoteListBox.PreviewMouseWheel -= QuoteListBox_PreviewMouseWheel;
  208. this.QuoteListBox.SelectionChanged -= QuoteListBox_SelectionChanged;
  209. this.QuoteListBox.KeyUp -= QuoteListBox_KeyUp;
  210. QuoteListBox.Resources.Clear();
  211. QuoteListBox = null;
  212. }
  213. this.Resources.Clear();
  214. GC.Collect();
  215. }
  216. #endregion
  217. #region IQuoteView
  218. /// <summary>
  219. /// 选中商品
  220. /// </summary>
  221. /// <value>The selected item.</value>
  222. /// <exception cref="System.NotImplementedException">
  223. /// </exception>
  224. public QuoteGoods SelectedItem
  225. {
  226. get
  227. {
  228. return QuoteListBox.SelectedItem as QuoteGoods;
  229. }
  230. }
  231. /// <summary>
  232. /// 第一个商品
  233. /// </summary>
  234. /// <value>The first quote goods.</value>
  235. /// <exception cref="System.NotImplementedException">
  236. /// </exception>
  237. public QuoteGoods FirstQuoteGoods
  238. {
  239. get
  240. {
  241. if (QuoteListBox.Items.Count == 0) return null;
  242. return QuoteListBox.Items[0] as QuoteGoods;
  243. }
  244. }
  245. /// <summary>
  246. /// 更新列表数据
  247. /// </summary>
  248. /// <param name="newType">The new type.</param>
  249. /// <param name="goodsGroup">The goods group.</param>
  250. public void RefreshList(MenuCommandType newType, MarketsInfoModel goodsGroup)
  251. {
  252. _currentType = newType;
  253. _goodsGroup = goodsGroup;
  254. if (ViewModel != null)
  255. {
  256. ViewModel.RefreshList(_currentType, _goodsGroup);
  257. }
  258. QuoteListBox.SelectedIndex = 0;
  259. }
  260. #endregion
  261. private void DecimalUpDown_KeyDown(object sender, KeyEventArgs e)
  262. {
  263. //屏蔽非法字符
  264. if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || (e.Key >= Key.D0 && e.Key <= Key.D9))
  265. {
  266. e.Handled = false;
  267. }
  268. else
  269. {
  270. e.Handled = true;
  271. }
  272. }
  273. public bool IsDeliveryVisible
  274. {
  275. get
  276. {
  277. return ApplicationParameter.IsDeliveryVisible == 1 && UserManager.IsAccountLogin;
  278. }
  279. }
  280. }
  281. }