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
{
///
/// QuerySaleGoodsView.xaml 的交互逻辑
///
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
///
/// 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.SaleDataGrid);
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(QuerySaleGoodsView), new PropertyMetadata(Visibility.Collapsed));
#endregion
#region Public Methods
///
/// Initializes a new instance of the class.
///
public QuerySaleGoodsView()
{
InitializeComponent();
_systemService = SimpleIoc.Default.GetInstance();
BuildDataGridColumns();
CurrentDataGrid = this.SaleDataGrid;
RegisterDataGridEvents();
}
///
/// 执行与释放或重置非托管资源相关的应用程序定义的任务。
///
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
///
/// Registers the data grid events.
///
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(this, MessengerTokens.RefreshGoodsToken, (msg) =>
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
var viewModel = this.DataContext as QuerySaleGoodsViewModel;
////刷新当前列表
if (viewModel != null)
{
viewModel.QuerySaleGoods();
}
}));
});
MessengerHelper.DefaultRegister(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);
}
///
/// Loads the account base information callback.
///
/// The account information.
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);
}));
}
///
/// 滚动按钮滚动时
///
/// The instance containing the event data.
private void OnScrollButtonClicked(EventArgs e)
{
if (ScrollButtonClicked != null)
{
ScrollButtonClicked(null, e);
}
}
///
/// 保存列表抬头
///
private void SaveQuoteHeaderList()
{
////保存列表抬头
var headers = QuoteDataGridHelper.GetHeadersFromDataGrid(SaleDataGrid);
if (headers != null && headers.Any()) UserManager.SaveQuoteListHeaders(QuoteListHeaderType.Sale, headers);
}
#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 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;
}
}
///
/// Handles the OnPreviewMouseWheel event of the SaleDataGrid control.
///
/// The source of the event.
/// The instance containing the event data.
private void SaleDataGrid_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 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