PgMeMenu.xaml.cs 5.32 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
using MultiLayout.UiModule;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
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;

namespace MultiLayout.MainEdit
{
    /// <summary>
    /// PgMeMenu.xaml 的交互逻辑
    /// </summary>
    public partial class PgMeMenu : Page
    {
        FlyLayout layout;
        FlyLayoutManager manager;
        GageTabItem gageTabItem;

        ObservableCollection<MeComponentType> ToolboxItems;
        ObservableCollection<MeFlyComponent> Items;
        public PgMeMenu()
        {
            InitializeComponent();
        }
        public void Init(GageTabItem gageTabItem, FlyLayoutManager manager)
        {
            this.manager = manager;
            layout = manager.layout;
            this.gageTabItem = gageTabItem;
            this.DataContext = gageTabItem;
            ToolboxItems = GraphConverter.GetToolboxItems(this.manager, ComponentType.Menu);
            Items = GraphConverter.GetMeFlyComponents(gageTabItem, gageTabItem.MenuItems, ToolboxItems);

            itemsControl_toolbox.ItemsSource = ToolboxItems;
            UpdateMenu();
        }

        void UpdateMenu()
        {
            UpdateStackpanel(spMenu, Items);
            Items.CollectionChanged += Items_CollectionChanged;
            UpdateComponentTypeCount();
        }
        private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case System.Collections.Specialized.NotifyCollectionChangedAction.Add:
                    {
                        foreach (MeFlyComponent c in e.NewItems)
                        {
                            var gct = c.Type;
                            gct.Count++;
                            ButtonComponentAdd(spMenu, c);
                        }
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    {
                        Button button = spMenu.Children[e.OldStartingIndex] as Button;
                        spMenu.Children.RemoveAt(e.OldStartingIndex);
                        spMenu.Children.Insert(e.NewStartingIndex, button);
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    {
                        foreach (MeFlyComponent c in e.OldItems)
                        {
                            var gct = c.Type;
                            gct.Count--;
                            Button button = c.mButton;
                            spMenu.Children.Remove(button);
                        }
                    }
                    break;
            }
        }

        void UpdateComponentTypeCount()
        {
            foreach (var type in ToolboxItems)
            {
                type.Count = Items.Count((c) => { return c.Type == type; });
            }
        }

        void UpdateStackpanel(StackPanel stackpanel, ObservableCollection<MeFlyComponent> components)
        {
            stackpanel.Children.Clear();

            //添加 控件
            for (int i = 0; i < components.Count(); i++)
            {
                var c = components[i];
                ButtonComponentAdd(stackpanel, c);
            }
        }

        void ButtonComponentAdd(StackPanel stackpanel, MeFlyComponent c)
        {
            Button b = new Button();
            c.mButton = b;
            b.Background = c.Type.Background;
            b.Click += button_componentEdit_Click;
            b.DataContext = c;
            b.Tag = c;
            stackpanel.Children.Add(b);
        }

        private void button_componentNew_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            var gct = button.Tag as MeComponentType;
            if (!gct.IsEnable)
            {
                string strTipCantAddAgain = "不能再次添加";
                FLY.ControlLibrary.MyMessageBox.Show(strTipCantAddAgain);
                return;
            }
            var c = new MeFlyComponent()
            {
                Type = gct,
                ServiceContainerName = gageTabItem.ServiceContainerName
            };
            Items.Add(c);
        }

        private void button_componentEdit_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
            var c = button.Tag as MeFlyComponent;

            int idx = Items.IndexOf(c);
            if (idx == -1)
            {
                //异常
                return;
            }
149
            WdMenuConfig w = new WdMenuConfig();
潘栩锋's avatar
潘栩锋 committed
150 151 152 153 154 155 156 157 158 159 160 161
            w.Init(Items, idx, manager.serviceNames);
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }

        private void btnBackClick(object sender, RoutedEventArgs e)
        {
            GraphConverter.ToFlyComponents(Items, gageTabItem, gageTabItem.MenuItems);
            NavigationService.GoBack();
        }
    }
}