PgMainEditDynArea.xaml.cs 6.25 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
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;
潘栩锋's avatar
潘栩锋 committed
15 16
using MultiLayout;
using MultiLayout.UiModule;
潘栩锋's avatar
潘栩锋 committed
17

潘栩锋's avatar
潘栩锋 committed
18
namespace MultiLayout.MainEdit
潘栩锋's avatar
潘栩锋 committed
19 20 21 22
{
    /// <summary>
    /// Page_MainEditDynArea.xaml 的交互逻辑
    /// </summary>
23
    public partial class PgMainEditDynArea : Page
潘栩锋's avatar
潘栩锋 committed
24
    {
潘栩锋's avatar
潘栩锋 committed
25 26 27
        FlyLayout layout;
        FlyLayoutManager manager;
        GageTabItem gageTabItem;
潘栩锋's avatar
潘栩锋 committed
28

潘栩锋's avatar
潘栩锋 committed
29 30
        ObservableCollection<MeComponentType> ToolboxItems;
        ObservableCollection<MeFlyComponent> Items;
31
        public PgMainEditDynArea()
潘栩锋's avatar
潘栩锋 committed
32 33 34
        {
            InitializeComponent();
        }
潘栩锋's avatar
潘栩锋 committed
35
        public void Init(GageTabItem gageTabItem, FlyLayoutManager manager)
潘栩锋's avatar
潘栩锋 committed
36
        {
潘栩锋's avatar
潘栩锋 committed
37 38 39 40 41 42
            this.manager = manager;
            layout = manager.layout;
            this.gageTabItem = gageTabItem;
            this.DataContext = gageTabItem;
            ToolboxItems = GraphConverter.GetToolboxItems(this.manager, ComponentType.DynArea);
            Items = GraphConverter.GetMeFlyComponents(gageTabItem, gageTabItem.DynAreaItems, ToolboxItems);
潘栩锋's avatar
潘栩锋 committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59

            itemsControl_toolbox.ItemsSource = ToolboxItems;
            UpdateDynArea();
        }

        void UpdateDynArea()
        {
            UpdateStackpanel(stackpanel_dynArea, 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:
                    {
潘栩锋's avatar
潘栩锋 committed
60
                        foreach (MeFlyComponent c in e.NewItems)
潘栩锋's avatar
潘栩锋 committed
61
                        {
潘栩锋's avatar
潘栩锋 committed
62
                            var gct = c.Type;
潘栩锋's avatar
潘栩锋 committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76
                            gct.Count++;
                            ButtonComponentAdd(stackpanel_dynArea, c);
                        }
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Move:
                    {
                        Button button = stackpanel_dynArea.Children[e.OldStartingIndex] as Button;
                        stackpanel_dynArea.Children.RemoveAt(e.OldStartingIndex);
                        stackpanel_dynArea.Children.Insert(e.NewStartingIndex, button);
                    }
                    break;
                case System.Collections.Specialized.NotifyCollectionChangedAction.Remove:
                    {
潘栩锋's avatar
潘栩锋 committed
77
                        foreach (MeFlyComponent c in e.OldItems)
潘栩锋's avatar
潘栩锋 committed
78
                        {
潘栩锋's avatar
潘栩锋 committed
79
                            var gct = c.Type;
潘栩锋's avatar
潘栩锋 committed
80 81 82 83 84 85 86 87 88 89 90
                            gct.Count--;
                            Button button = c.mButton;
                            stackpanel_dynArea.Children.Remove(button);
                        }
                    }
                    break;
            }
        }

        void UpdateComponentTypeCount()
        {
潘栩锋's avatar
潘栩锋 committed
91
            foreach (var type in ToolboxItems)
潘栩锋's avatar
潘栩锋 committed
92 93 94 95 96
            {
                type.Count = Items.Count((c) => { return c.Type == type; });
            }
        }

潘栩锋's avatar
潘栩锋 committed
97
        void UpdateStackpanel(StackPanel stackpanel, ObservableCollection<MeFlyComponent> components)
潘栩锋's avatar
潘栩锋 committed
98 99 100 101 102 103
        {
            stackpanel.Children.Clear();

            //添加 控件
            for (int i = 0; i < components.Count(); i++)
            {
潘栩锋's avatar
潘栩锋 committed
104
                var c = components[i];
潘栩锋's avatar
潘栩锋 committed
105 106 107 108
                ButtonComponentAdd(stackpanel, c);
            }
        }

潘栩锋's avatar
潘栩锋 committed
109
        void ButtonComponentAdd(StackPanel stackpanel, MeFlyComponent c)
潘栩锋's avatar
潘栩锋 committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        {
            Button b = new Button();
            c.mButton = b;
            b.Name = "button_component";
            b.Style = this.Resources["ButtonStyle_component"] as Style;
            b.Background = c.Type.Background;
            b.Click += button_componentEdit_Click;
            b.DataContext = c;
            b.Tag = c;
            stackpanel.Children.Add(b);
        }

        private void button_componentDel_Click(object sender, RoutedEventArgs e)
        {
            e.Handled = true;
            //向上找TabItem
            Button button = FLY.ControlLibrary.COMMON.GetParent<Button>(sender as DependencyObject, "button_component");
            if (button == null)
            {
                //异常
                return;
            }
潘栩锋's avatar
潘栩锋 committed
132 133
            //string strTipComponentDel = Application.Current.FindResource("strTipComponentDel") as string;
            if (FLY.ControlLibrary.MyMessageBox.Show("组件将要被删除,是否确定?") == true)
潘栩锋's avatar
潘栩锋 committed
134
            {
潘栩锋's avatar
潘栩锋 committed
135
                MeFlyComponent c = button.Tag as MeFlyComponent;
潘栩锋's avatar
潘栩锋 committed
136 137 138 139 140 141 142
                Items.Remove(c);
            }
        }

        private void button_componentNew_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
潘栩锋's avatar
潘栩锋 committed
143
            var gct = button.Tag as MeComponentType;
潘栩锋's avatar
潘栩锋 committed
144 145
            if (!gct.IsEnable)
            {
潘栩锋's avatar
潘栩锋 committed
146
                string strTipCantAddAgain = "不能再次添加";
潘栩锋's avatar
潘栩锋 committed
147 148 149
                FLY.ControlLibrary.MyMessageBox.Show(strTipCantAddAgain);
                return;
            }
潘栩锋's avatar
潘栩锋 committed
150
            var c = new MeFlyComponent()
潘栩锋's avatar
潘栩锋 committed
151
            {
潘栩锋's avatar
潘栩锋 committed
152 153
                Type = gct,
                ServiceContainerName = gageTabItem.ServiceContainerName
潘栩锋's avatar
潘栩锋 committed
154 155 156 157 158 159 160
            };
            Items.Add(c);
        }

        private void button_componentEdit_Click(object sender, RoutedEventArgs e)
        {
            Button button = sender as Button;
潘栩锋's avatar
潘栩锋 committed
161
            var c = button.Tag as MeFlyComponent;
潘栩锋's avatar
潘栩锋 committed
162 163 164 165 166 167 168

            int idx = Items.IndexOf(c);
            if (idx == -1)
            {
                //异常
                return;
            }
169
            WdDynAreaConfig w = new WdDynAreaConfig();
潘栩锋's avatar
潘栩锋 committed
170
            w.Init(Items, idx, manager.serviceNames);
潘栩锋's avatar
潘栩锋 committed
171 172 173
            w.Owner = FLY.ControlLibrary.COMMON.GetWindow(this);
            w.ShowDialog();
        }
潘栩锋's avatar
潘栩锋 committed
174 175 176 177 178 179

        private void btnBackClick(object sender, RoutedEventArgs e)
        {
            GraphConverter.ToFlyComponents(Items, gageTabItem, gageTabItem.DynAreaItems);
            NavigationService.GoBack();
        }
潘栩锋's avatar
潘栩锋 committed
180 181
    }
}