using System;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.UI.ViewModels;
using System.Windows.Controls;
using System.Windows.Input;
namespace Muchinfo.MTPClient.UI.Views
{
///
/// SystemMenuTree.xaml 的交互逻辑
///
public partial class SystemMenuTree : UserControl
{
private SysMenuItem _oldSysMenuItem = null;
public SystemMenuTree()
{
InitializeComponent();
// trees.InitTreeview();
MessengerHelper.DefaultRegister(this, MessengerTokens.MenuSelectChange, (menuCommand) =>
{
var viewModel= this.DataContext as MenuTreeViewModel;
if (viewModel != null)
{
var menuItem= viewModel.FindSystemMenuItem(menuCommand);
if (menuItem != null)
{
menuItem.IsSelected = true;
TreeViewClick(menuItem);
}
}
});
this.Unloaded += SystemMenuTree_Unloaded;
}
void SystemMenuTree_Unloaded(object sender, System.Windows.RoutedEventArgs e)
{
MessengerHelper.DefaultUnregister(this);
}
private void TreeViewClick(SysMenuItem sysItem)
{
if (sysItem != null && sysItem.Command != null && sysItem.IsEnabled)
{
////点击买入、卖出、撤单时,选中不变
if (sysItem.SysCommandType == MenuCommandType.BuyIn || sysItem.SysCommandType == MenuCommandType.Sellout || sysItem.SysCommandType == MenuCommandType.Cancel)
{
sysItem.IsSelected = false;
if (_oldSysMenuItem != null) _oldSysMenuItem.IsSelected = true;
}
else
{
_oldSysMenuItem = sysItem;
}
sysItem.Command.Execute(sysItem);
}
}
private void TreeView_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
TreeViewClick( FunctionTreeView.SelectedItem as SysMenuItem);
}
}
}