using System.Windows.Media.Animation;
using Muchinfo.MTPClient.Data.DTO;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Data.Model;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Infrastructure.Interfaces;
using Muchinfo.MTPClient.Quotation.ViewModels;
using Muchinfo.PC.Common.Helpers;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Muchinfo.MTPClient.Infrastructure.Cache;
using System.Linq;
using Muchinfo.MTPClient.IService;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Infrastructure.Utilities;
namespace Muchinfo.MTPClient.Quotation.Views
{
///
/// QuoteBoardView.xaml 的交互逻辑
///
public partial class QuoteBoardView : UserControl, IDisposable, IQuoteView
{
#region Fields
private const double Const_ScrollWidth = 60d;
private MenuCommandType _currentType;
private string _additionalInfo;
private MarketsInfoModel _goodsGroup;
private ISystemService _systemService;
private ScrollViewer _datagridScrollViewer; //当前行情DataGrid滚动条
#endregion
#region Properties
private QuoteBoardViewModel ViewModel
{
get
{
return this.DataContext as QuoteBoardViewModel;
}
}
public ScrollViewer DataGridScrollViewer
{
get
{
if (_datagridScrollViewer != null)
{
return _datagridScrollViewer;
}
_datagridScrollViewer = WPFVisualTreeHelper.FindVisualChild(this.QuoteListBox);
return _datagridScrollViewer;
}
}
#endregion
#region Constructors
public QuoteBoardView(MenuCommandType commandType, MarketsInfoModel additionalInfo)
{
InitializeComponent();
_systemService = SimpleIoc.Default.GetInstance();
this.DataContext = new QuoteBoardViewModel();
RefreshList(commandType, additionalInfo);
_goodsGroup = additionalInfo;
this.QuoteListBox.PreviewMouseWheel += QuoteListBox_PreviewMouseWheel;
this.QuoteListBox.SelectionChanged += QuoteListBox_SelectionChanged;
this.QuoteListBox.KeyUp += QuoteListBox_KeyUp;
MessengerHelper.DefaultRegister(this, MessengerTokens.QuoteBoardSelectItemChange, (goods) =>
{
if (QuoteListBox == null) return;
QuoteListBox.SelectedItem = goods;
});
MessengerHelper.DefaultRegister(this, MessengerTokens.RefreshGoodsToken, (msg) =>
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
RefreshList(_currentType, _goodsGroup);
}));
});
this.Unloaded += QuoteBoardView_Unloaded;
if (!IsDeliveryVisible)
{
this.PAndXGoodsInfoDataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden;
}
}
///
/// 删除注册内容
///
///
///
void QuoteBoardView_Unloaded(object sender, RoutedEventArgs e)
{
//MessengerHelper.DefaultUnregister(MessengerTokens.RefreshGoodsToken);
//MessengerHelper.DefaultUnregister(MessengerTokens.RegisterResourceChange);
//MessengerHelper.DefaultUnregister(MessengerTokens.QuoteBoardSelectItemChange);
//Dispose();
}
#endregion
#region Event Handlers
///
/// Handles the KeyUp event of the QuoteListBox control.
///
/// The source of the event.
/// The instance containing the event data.
private void QuoteListBox_KeyUp(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 SelectionChanged event of the QuoteListBox control.
///
/// The source of the event.
/// The instance containing the event data.
private void QuoteListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems != null && e.AddedItems.Count > 0)
{
var quoteGoods = e.AddedItems[0] as QuoteGoodsDTO;
if (quoteGoods != null)
{
MessengerHelper.DefaultSend(quoteGoods, MessengerTokens.SelectGoodsChange);
if (ViewModel != null)
{
ViewModel.SelectGoodsOfPAndX_ItemSource(quoteGoods);
if (ViewModel.GoodsOfPandXInfoList != null)
{
var fistGoods = ViewModel.GoodsOfPandXInfoList.FirstOrDefault();
if (fistGoods != null)
{
var group = CacheManager.CacheMarketsGroups.FirstOrDefault(
(item) => item.MarketID == fistGoods.GoodsGroupId);
BuildDeliveryDataGridColumns(group);
}
}
}
}
}
}
#region 交割行情列表
private void BuildDeliveryDataGridColumns(MarketsInfoModel goosGroup)
{
var type = GetQuoteListHeaderTypeByeMarket(goosGroup);
var headerList = _systemService.GetDeliveryQuoteListHeaders(type);
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;
////清除所有列
PAndXGoodsInfoDataGrid.Columns.Clear();
////添加列
foreach (var column in columns)
{
PAndXGoodsInfoDataGrid.Columns.Add(column);
}
}
#endregion
///
/// 根据市场获取列头
///
/// The goods group.
/// MarketType.
private eTradeMode GetQuoteListHeaderTypeByeMarket(MarketsInfoModel goodsGroup)
{
if (goodsGroup == null)
{
return eTradeMode.TRADEMODE_MARKETMAKE;
}
return goodsGroup.TradeMode;
}
///
/// Handles the PreviewMouseWheel event of the QuoteListBox control.
///
/// The source of the event.
/// The instance containing the event data.
private void QuoteListBox_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
{
if (e.Delta > 0)
{
DataGridScrollViewer.PageUp();
}
if (e.Delta < 0)
{
DataGridScrollViewer.PageDown();
}
e.Handled = true;
}
#endregion
#region IDispose
///
/// 执行与释放或重置非托管资源相关的应用程序定义的任务。
///
public void Dispose()
{
MessengerHelper.DefaultUnregister(this);
if (ViewModel != null) ViewModel.Cleanup();
if (QuoteListBox != null)
{
this.QuoteListBox.PreviewMouseWheel -= QuoteListBox_PreviewMouseWheel;
this.QuoteListBox.SelectionChanged -= QuoteListBox_SelectionChanged;
this.QuoteListBox.KeyUp -= QuoteListBox_KeyUp;
QuoteListBox.Resources.Clear();
QuoteListBox = null;
}
this.Resources.Clear();
GC.Collect();
}
#endregion
#region IQuoteView
///
/// 选中商品
///
/// The selected item.
///
///
public QuoteGoods SelectedItem
{
get
{
return QuoteListBox.SelectedItem as QuoteGoods;
}
}
///
/// 第一个商品
///
/// The first quote goods.
///
///
public QuoteGoods FirstQuoteGoods
{
get
{
if (QuoteListBox.Items.Count == 0) return null;
return QuoteListBox.Items[0] as QuoteGoods;
}
}
///
/// 更新列表数据
///
/// The new type.
/// The goods group.
public void RefreshList(MenuCommandType newType, MarketsInfoModel goodsGroup)
{
_currentType = newType;
_goodsGroup = goodsGroup;
if (ViewModel != null)
{
ViewModel.RefreshList(_currentType, _goodsGroup);
}
QuoteListBox.SelectedIndex = 0;
}
#endregion
private void DecimalUpDown_KeyDown(object sender, KeyEventArgs e)
{
//屏蔽非法字符
if ((e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || (e.Key >= Key.D0 && e.Key <= Key.D9))
{
e.Handled = false;
}
else
{
e.Handled = true;
}
}
public bool IsDeliveryVisible
{
get
{
return ApplicationParameter.IsDeliveryVisible == 1 && UserManager.IsAccountLogin;
}
}
}
}