using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using GalaSoft.MvvmLight.Ioc; using Muchinfo.MTPClient.Data.Enums; using Muchinfo.MTPClient.Data.Model; using Muchinfo.MTPClient.Infrastructure.Helpers; 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; namespace Muchinfo.MTPClient.Sale.Views { /// /// DepositPlanView.xaml 的交互逻辑 /// public partial class DepositPlanView : UserControl { public EventHandler ScrollButtonClicked; #region Private Fields private const double Const_ScrollWidth = 60d; private ScrollViewer _datagridScrollViewer; //当前DataGrid滚动条 private ISystemService _systemService; #endregion #region Public Properties /// /// Gets or sets the current data grid. /// /// The current data grid. public WPF.Controls.DataGrid.MuchinfoDataGrid CurrentDataGrid { get; set; } /// /// 获取当前理财DataGrid的滚动条 /// public ScrollViewer DataGridScrollViewer { get { if (_datagridScrollViewer != null) { return _datagridScrollViewer; } _datagridScrollViewer = WPFVisualTreeHelper.FindVisualChild(this.DepositDataGrid); return _datagridScrollViewer; } } #endregion #region Dependency Properties /// /// 是否显示左右滚动条 /// 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(DepositPlanView), new PropertyMetadata(Visibility.Collapsed)); #endregion #region Public Methods /// /// Initializes a new instance of the class. /// public DepositPlanView() { InitializeComponent(); _systemService = SimpleIoc.Default.GetInstance(); this.DataContext = new DepositPlanViewModel(); BuildDataGridColumns(); CurrentDataGrid = this.DepositDataGrid; RegisterDataGridEvents(); } /// /// 执行与释放或重置非托管资源相关的应用程序定义的任务。 /// public void Dispose() { 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 /// /// Registers the data grid events. /// private void RegisterDataGridEvents() { this.DepositDataGrid.LayoutUpdated += (s, e) => { OnScrollButtonClicked(null); if (DataGridScrollViewer == null) return; var canScroll = DataGridScrollViewer.ScrollableWidth == 0; ScrollVisibility = canScroll ? Visibility.Collapsed : Visibility.Visible; }; this.Unloaded += QuerySaleGoodsView_Unloaded; } private void BuildDataGridColumns() { var headerList = _systemService.GetQuoteListHeaders(eTradeMode.Deposit); 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; ////清除所有列 DepositDataGrid.Columns.Clear(); ////添加列 foreach (var column in columns) { DepositDataGrid.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), }; DepositDataGrid.Columns.Add(operationColumn); } /// /// 滚动按钮滚动时 /// /// The instance containing the event data. private void OnScrollButtonClicked(EventArgs e) { if (ScrollButtonClicked != null) { ScrollButtonClicked(null, e); } } #region Route Events /// /// Handles the Unloaded event of the QuerySaleGoodsView control. /// /// The source of the event. /// The instance containing the event data. private void QuerySaleGoodsView_Unloaded(object sender, RoutedEventArgs e) { // Dispose(); } /// /// Handles the OnKeyDown event of the SaleDataGrid control. /// /// The source of the event. /// The instance containing the event data. private void DepositDataGrid_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; } } /// /// Handles the OnPreviewMouseWheel event of the SaleDataGrid control. /// /// The source of the event. /// The instance containing the event data. private void DepositDataGrid_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e) { if (e.Delta > 0) { DataGridScrollViewer.PageUp(); } if (e.Delta < 0) { DataGridScrollViewer.PageDown(); } e.Handled = true; } /// /// Handles the OnMouseRightButtonDown event of the SaleDataGrid control. /// /// The source of the event. /// The instance containing the event data. private void DepositDataGrid_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 rowContextMenu = new ContextMenu(); var menuItemStyle = ResourceHelper.GetFromeResource