using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Data.Model;
using Muchinfo.MTPClient.Infrastructure.Cache;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Trade.ViewModels;
namespace Muchinfo.MTPClient.Trade.Views
{
///
/// OrderContent.xaml 的交互逻辑
///
public partial class OrderContent : UserControl
{
private List _quoteGoodses;
public OrderContent()
{
// ViewModel -> TradeOrderViewModel
InitializeComponent();
//this.Bor_CommissionsHeight.Height = 280;
MessengerHelper.DefaultRegister(this, MessengerTokens.SetOrderFoucsToken, (e) =>
{
if (this.IsVisible)
{
goodsComBoxBox.Focus();
}
});
setFocusList.Add(goodsComBoxBox);
setFocusList.Add(DecimalNum);
setFocusList.Add(PriceNumBox);
setFocusList.Add(btnMouseDown);
goodsComBoxBox.GotFocus += goodsComBoxBox_GotFocus;
DecimalNum.GotFocus += goodsComBoxBox_GotFocus;
PriceNumBox.GotFocus += goodsComBoxBox_GotFocus;
btnMouseDown.GotFocus += goodsComBoxBox_GotFocus;
direction.Visibility = Visibility.Collapsed;
goodsComBoxBox.DropDownClosed += goodsComBoxBox_DropDownClosed;
MessengerHelper.DefaultRegister(this, MessengerTokens.TradeLoadGoods, (e) =>
{
if (OrderContentArea.Visibility != Visibility.Visible)
{
OrderContentArea.Visibility = Visibility.Visible;
}
});
}
void goodsComBoxBox_DropDownClosed(object sender, EventArgs e)
{
var viewModel = this.DataContext as TradeOrderViewModel;
if (viewModel != null && _quoteGoodses != null)
{
viewModel.GoodsList = _quoteGoodses;
}
}
#region 传统模式时调用修改五档高度
public string TraditionCommissionsHeight
{
get { return (string)GetValue(TraditionCommissionsHeightProperty); }
set { SetValue(TraditionCommissionsHeightProperty, value); }
}
// Using a DependencyProperty as the backing store for ShowText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TraditionCommissionsHeightProperty =
DependencyProperty.Register("TraditionCommissionsHeight", typeof(string), typeof(OrderContent), new PropertyMetadata("", new PropertyChangedCallback(TCHPropertyChangedCallback)));
private static void TCHPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs arg)
{
if (sender != null && sender is OrderContent)
{
var windowContent = (OrderContent)sender;
windowContent.Bor_CommissionsHeight.Height = double.Parse(windowContent.TraditionCommissionsHeight);
}
}
#endregion
void goodsComBoxBox_GotFocus(object sender, RoutedEventArgs e)
{
var index = setFocusList.FindIndex((item)=>sender.Equals(item) );
if (index >= 0)
{
focusIndex = index;
}
}
private void OrderContent_OnMouseDown(object sender, MouseButtonEventArgs e)
{
if (goodsComBoxBox.IsVisible)
{
goodsComBoxBox.Focus();
e.Handled = true;
}
}
List setFocusList=new List();
private int focusIndex = 0;
private void OrderContent_OnKeyUp(object sender, KeyEventArgs e)
{
if (this.IsKeyboardFocusWithin)
{
switch (e.Key)
{
case Key.Enter:
//if (goodsComBoxBox.IsFocused)
//{
// DecimalNum.Focus();
//}
//else if (DecimalNum.IsKeyboardFocusWithin)
//{
// if (PriceNumBox.IsVisible)
// {
// PriceNumBox.Focus();
// }
// else
// {
// if (btnMouseDown.IsVisible)
// {
// btnMouseDown.Focus();
// }
// else
// {
// MessengerHelper.DefaultSend(true, MessengerTokens.SetButtonFoucsToken);
// }
// }
//}
//else if (PriceNumBox.IsKeyboardFocusWithin &&PriceNumBox.IsVisible)
//{
// if (btnMouseDown.IsVisible)
// {
// btnMouseDown.Focus();
// }
// else
// {
// MessengerHelper.DefaultSend(true, MessengerTokens.SetButtonFoucsToken);
// }
//}
//else
//{
// goodsComBoxBox.Focus();
//}
SetContrlFocus(1);
break;
// 为什么要搞这些操作?界面会崩溃,我还是去掉它吧
/*case Key.Up:
SetContrlFocus(-1);
e.Handled = true;
break;
case Key.Down:
SetContrlFocus(1);
e.Handled = true;
break;*/
default:
break;
}
}
}
private string goodsBoxString = "";
///
/// 设置焦点
///
///
private void SetContrlFocus(int next)
{
var index = focusIndex+next;
if (index > setFocusList.Count - 1)
{
setFocusList[0].Focus();
}
else if (next < 0)
{
setFocusList[3].Focus();
if (!btnMouseDown.IsVisible)
{
focusIndex = 3;
MessengerHelper.DefaultSend(true, MessengerTokens.SetButtonFoucsToken);
}
//{
// MessengerHelper.DefaultSend(true, MessengerTokens.SetButtonFoucsToken);
// // focusIndex = 3;
//}
}
if (index == 2 && !PriceNumBox.IsVisible)
{
index+= next;
}
if (index >= setFocusList.Count)
{
return;
}
setFocusList[index].Focus();
if (!btnMouseDown.IsVisible && index==3)
{
focusIndex = 3;
MessengerHelper.DefaultSend(true, MessengerTokens.SetButtonFoucsToken);
}
}
private void UIElement_OnMouseDown(object sender, MouseButtonEventArgs e)
{
var viewModel = this.DataContext as TradeOrderViewModel;
var element = sender as FrameworkElement;
if (element != null &&viewModel!=null)
{
if (element.Tag is decimal)
{
viewModel.SetOrderPrice((decimal)element.Tag);
}
// element.Tag
}
}
private void BidCommissionsListBox_MouseLeave(object sender, MouseEventArgs e)
{
this.BidCommissionsListBox.SelectedIndex = -1;
}
private void AskCommissionsListBox_MouseLeave(object sender, MouseEventArgs e)
{
this.AskCommissionsListBox.SelectedIndex = -1;
}
///
/// 商品栏输入商品的时候
///
///
///
private void goodsComBoxBox_KeyUp(object sender, KeyEventArgs e)
{
if (this.IsKeyboardFocusWithin)
{
switch (e.Key)
{
//暂时似乎没有其他的指令需要处理
default:
{
if (!goodsBoxString.Equals(goodsComBoxBox.Text))
{
goodsBoxString = goodsComBoxBox.Text;
}
else
{
return;
}
var viewModel = this.DataContext as TradeOrderViewModel;
if (_quoteGoodses == null && viewModel != null)// 在关闭dropdown的时候,要用到原来的值
{
_quoteGoodses = viewModel.GoodsList;
}
var goods = CacheManager.CacheGoodsBaseInfos.FindAll(i => (i.GoodsCode.Contains(goodsBoxString) || i.Name.Contains(goodsBoxString))); // 过滤匹配的商品
if (viewModel != null)
{
viewModel.GoodsList = goods;// 给绑定的空间丢数据
}
goodsComBoxBox.IsDropDownOpen = goods.Count > 0;// 有数据就显示,没有数据就没有必要显示了。
break;
}
}
}
}
}
}