| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384 |
- using GalaSoft.MvvmLight.Ioc;
- using Muchinfo.MTPClient.Data.Enums;
- using Muchinfo.MTPClient.Data.Model;
- using Muchinfo.MTPClient.Infrastructure.Helpers;
- using Muchinfo.MTPClient.Infrastructure.LinkProxy;
- using Muchinfo.MTPClient.Infrastructure.Utilities;
- using Muchinfo.MTPClient.IService;
- using Muchinfo.MTPClient.Resources;
- using Muchinfo.MTPClient.Sale.ViewModels;
- using Muchinfo.MTPClient.Service.Utilities;
- using Muchinfo.PC.Common.Helpers;
- using System;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using System.Windows.Media;
- namespace Muchinfo.MTPClient.Sale.Views
- {
- /// <summary>
- /// QuerySaleGoodsView.xaml 的交互逻辑
- /// </summary>
- public partial class QuerySaleGoodsView : UserControl, IDisposable
- {
- public EventHandler ScrollButtonClicked;
- #region Private Fields
- private const double Const_ScrollWidth = 60d;
- private ScrollViewer _datagridScrollViewer; //当前DataGrid滚动条
- private ISystemService _systemService;
- #endregion
- #region Public Properties
- /// <summary>
- /// Gets or sets the current data grid.
- /// </summary>
- /// <value>The current data grid.</value>
- public WPF.Controls.DataGrid.MuchinfoDataGrid CurrentDataGrid
- {
- get;
- set;
- }
- /// <summary>
- /// 获取当前理财DataGrid的滚动条
- /// </summary>
- public ScrollViewer DataGridScrollViewer
- {
- get
- {
- if (_datagridScrollViewer != null)
- {
- return _datagridScrollViewer;
- }
- _datagridScrollViewer = WPFVisualTreeHelper.FindVisualChild<ScrollViewer>(this.SaleDataGrid);
- return _datagridScrollViewer;
- }
- }
- #endregion
- #region Dependency Properties
- /// <summary>
- /// 是否显示左右滚动条
- /// </summary>
- public Visibility ScrollVisibility
- {
- get { return (Visibility)GetValue(ScrollVisibilityProperty); }
- set { SetValue(ScrollVisibilityProperty, value); }
- }
- // Using a DependencyProperty as the backing store for ScrollVisibility. This enables animation, styling, binding, etc...
- public static readonly DependencyProperty ScrollVisibilityProperty = DependencyProperty.Register("ScrollVisibility", typeof(Visibility), typeof(QuerySaleGoodsView), new PropertyMetadata(Visibility.Collapsed));
- #endregion
- #region Public Methods
- /// <summary>
- /// Initializes a new instance of the <see cref="QuerySaleGoodsView"/> class.
- /// </summary>
- public QuerySaleGoodsView()
- {
- InitializeComponent();
- _systemService = SimpleIoc.Default.GetInstance<ISystemService>();
- BuildDataGridColumns();
- CurrentDataGrid = this.SaleDataGrid;
- RegisterDataGridEvents();
- }
- /// <summary>
- /// 执行与释放或重置非托管资源相关的应用程序定义的任务。
- /// </summary>
- public void Dispose()
- {
- ////保存抬头
- SaveQuoteHeaderList();
- MessengerHelper.DefaultUnregister(this);
- var viewModel = this.DataContext as QuerySaleGoodsViewModel;
- if (viewModel != null)
- {
- viewModel.Cleanup();
- if (_datagridScrollViewer != null)
- {
- _datagridScrollViewer.Resources.Clear();
- _datagridScrollViewer = null;
- }
- }
- this.Resources.Clear();
- GC.Collect();
- }
- #endregion
- #region Private Methods
- /// <summary>
- /// Registers the data grid events.
- /// </summary>
- private void RegisterDataGridEvents()
- {
- this.SaleDataGrid.LayoutUpdated += (s, e) =>
- {
- OnScrollButtonClicked(null);
- if (DataGridScrollViewer == null) return;
- var canScroll = DataGridScrollViewer.ScrollableWidth == 0;
- ScrollVisibility = canScroll ? Visibility.Collapsed : Visibility.Visible;
- };
- MessengerHelper.DefaultRegister<string>(this, MessengerTokens.RefreshGoodsToken, (msg) =>
- {
- Application.Current.Dispatcher.BeginInvoke(new Action(() =>
- {
- var viewModel = this.DataContext as QuerySaleGoodsViewModel;
- ////刷新当前列表
- if (viewModel != null)
- {
- viewModel.QuerySaleGoods();
- }
- }));
- });
- MessengerHelper.DefaultRegister<string>(this, MessengerTokens.SaveLayout, (s) =>
- {
- ////保存列表抬头
- SaveQuoteHeaderList();
- });
- this.Unloaded += QuerySaleGoodsView_Unloaded;
- }
- private void BuildDataGridColumns()
- {
- var headerList = _systemService.GetQuoteListHeaders(eTradeMode.TRADEMODE_SALE);
- var quoteListHeaders = (headerList == null) ? null : headerList.ToList();
- if (quoteListHeaders == null || !quoteListHeaders.Any()) return;
- ////根据QuoteListHeader集合创建行情DataGrid列集合
- var columns = QuoteDataGridHelper.CreateQuoteDataGridColumns(quoteListHeaders);
- if (columns == null || !columns.Any()) return;
- ////清除所有列
- SaleDataGrid.Columns.Clear();
- ////添加列
- foreach (var column in columns)
- {
- SaleDataGrid.Columns.Add(column);
- }
- ////添加操作按钮列
- var operationStackPanel = new StackPanel()
- {
- HorizontalAlignment = HorizontalAlignment.Stretch
- };
- operationStackPanel.Children.Add(new TextBlock() { Text = Client_Resource.Content_Operation, HorizontalAlignment = HorizontalAlignment.Center });
- var operationDataTemplate = this.Resources["OperationDataTemplate"] as DataTemplate;
- var operationColumn = new QuoteDataGridTemplateColumn()
- {
- ////QuoteListHeader设为空,不保存此列
- QuoteListHeader = null,
- Header = operationStackPanel,
- Visibility = Visibility.Visible,
- CellTemplate = operationDataTemplate,
- Width = new DataGridLength(1, DataGridLengthUnitType.Auto),
- };
- SaleDataGrid.Columns.Add(operationColumn);
- }
- /// <summary>
- /// Loads the account base information callback.
- /// </summary>
- /// <param name="accountInfo">The account information.</param>
- private void LoadAccountBaseInfoCallback(AccountInfoEntity accountInfo)
- {
- this.Dispatcher.BeginInvoke(new Action(() =>
- {
- var viewModel = this.DataContext as QuerySaleGoodsViewModel;
- ////刷新当前列表
- if (viewModel != null)
- {
- viewModel.QuerySaleGoods();
- }
- ////重新订阅并查询盘面
- QuoteProxyHelper.QuoteSubscribe(GoodsFromScr.All);
- }));
- }
- /// <summary>
- /// 滚动按钮滚动时
- /// </summary>
- /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
- private void OnScrollButtonClicked(EventArgs e)
- {
- if (ScrollButtonClicked != null)
- {
- ScrollButtonClicked(null, e);
- }
- }
- /// <summary>
- /// 保存列表抬头
- /// </summary>
- private void SaveQuoteHeaderList()
- {
- ////保存列表抬头
- var headers = QuoteDataGridHelper.GetHeadersFromDataGrid(SaleDataGrid);
- if (headers != null && headers.Any()) UserManager.SaveQuoteListHeaders(QuoteListHeaderType.Sale, headers);
- }
- #region Route Events
- /// <summary>
- /// Handles the Unloaded event of the QuerySaleGoodsView control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
- private void QuerySaleGoodsView_Unloaded(object sender, RoutedEventArgs e)
- {
- // Dispose();
- }
- /// <summary>
- /// Handles the OnKeyDown event of the SaleDataGrid control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="KeyEventArgs"/> instance containing the event data.</param>
- private void SaleDataGrid_OnKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Left)
- {
- if (DataGridScrollViewer == null) return;
- DataGridScrollViewer.ScrollToHorizontalOffset(DataGridScrollViewer.HorizontalOffset - Const_ScrollWidth);
- e.Handled = true;
- }
- else if (e.Key == Key.Right)
- {
- if (DataGridScrollViewer == null) return;
- DataGridScrollViewer.ScrollToHorizontalOffset(DataGridScrollViewer.HorizontalOffset + Const_ScrollWidth);
- e.Handled = true;
- }
- }
- /// <summary>
- /// Handles the OnPreviewMouseWheel event of the SaleDataGrid control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseWheelEventArgs"/> instance containing the event data.</param>
- private void SaleDataGrid_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
- {
- if (e.Delta > 0)
- {
- DataGridScrollViewer.PageUp();
- }
- if (e.Delta < 0)
- {
- DataGridScrollViewer.PageDown();
- }
- e.Handled = true;
- }
- /// <summary>
- /// Handles the OnMouseRightButtonDown event of the SaleDataGrid control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="MouseButtonEventArgs"/> instance containing the event data.</param>
- private void SaleDataGrid_OnMouseRightButtonDown(object sender, MouseButtonEventArgs e)
- {
- var obj = e.OriginalSource as DependencyObject;
- while (obj != null && !(obj is DataGridColumnHeader))
- {
- obj = VisualTreeHelper.GetParent(obj);
- }
- var header = obj as DataGridColumnHeader;
- if (header != null)
- {
- ////注释掉表头左键菜单,使用统一菜单
- ////header.ContextMenu = _headerContextMenu;
- //var item = QuoteListDataGrid.SelectedItem as QuoteGoodsDTO;
- //if (item == null) return;
- //BuildContextMenu(item);
- }
- else
- {
- var row = e.OriginalSource as DependencyObject;
- while (row != null && !(row is DataGridRow))
- {
- row = VisualTreeHelper.GetParent(row);
- }
- var dataRow = row as DataGridRow;
- //if (dataRow != null)
- //{
- //var item = dataRow.Item as SaleGoods;
- //if (item == null) return;
- var rowContextMenu = new ContextMenu();
- var menuItemStyle = ResourceHelper.GetFromeResource<Style>("ContextCommonMenuItem");
- var menuItem = new MenuItem()
- {
- Header = ContextMenuNames.RefreshGoodsList,
- Tag = ContextMenuCommandType.RefreshGoodsList,
- //DataContext = item,
- Style = menuItemStyle,
- };
- menuItem.Click += RefreshGoodsList_Click;
- menuItem.IsEnabled = UserManager.IsAccountLogin; ////只有账号登录了才有效
- rowContextMenu.Items.Add(menuItem);
- this.SaleDataGrid.ContextMenu = rowContextMenu;
- //}
- //else
- //{
- // ////在DataGrid外点击右键,取消右键菜单
- // this.SaleDataGrid.ContextMenu = null;
- //}
- }
- }
- /// <summary>
- /// Handles the Click event of the RefreshGoodsList control.
- /// </summary>
- /// <param name="sender">The source of the event.</param>
- /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
- private void RefreshGoodsList_Click(object sender, RoutedEventArgs e)
- {
- var menuItem = sender as MenuItem;
- if (menuItem == null) return;
- var commandType = (ContextMenuCommandType)menuItem.Tag;
- switch (commandType)
- {
- case ContextMenuCommandType.RefreshGoodsList:
- ////刷新商品列表
- var loginService = SimpleIoc.Default.GetInstance<ILoginService>();
- if (loginService == null) return;
- ////获取账号及商品信息
- loginService.AccountBaseInfoInit(UserManager.CurrentTradeAccount.AccountId, LoadAccountBaseInfoCallback, null);
- ////查询交易参数配置
- loginService.SystemClientParamterConfig(null, null);
- var accountId = UserManager.CurrentTradeAccount.AccountId;
- //if (UserManager.CurrentTradeAccount.FundsAccounts.Any() &&
- // UserManager.CurrentTradeAccount.FundsAccounts[0] != null)
- //{
- // accountId = UserManager.CurrentTradeAccount.FundsAccounts[0].AccountId;
- //}
- ////获取商品参数
- loginService.QueryAccountGoodsParams(accountId, null, null);
- ////更新资金信息
- MessengerHelper.DefaultSend(UserManager.CurrentTradeAccount.AccountId + string.Empty, MessengerTokens.MoneyNoticeToken);
- break;
- }
- }
- #endregion
- #endregion
- }
- }
|