コード例 #1
0
        private MetroTabItem CreateTab(NPCCharacter character)
        {
            var binding = new Binding()
            {
                UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
                Mode = BindingMode.OneWay,
                Path = new PropertyPath("UIText")
            };
            Label l = new Label();

            l.SetBinding(Label.ContentProperty, binding);

            MetroTabItem tabItem = new MetroTabItem
            {
                CloseButtonEnabled = true,
                CloseTabCommand    = CloseTabCommand,
                Header             = l,
                DataContext        = character
            };

            tabItem.CloseTabCommandParameter = tabItem;

            var             cmenu      = new ContextMenu();
            List <MenuItem> cmenuItems = new List <MenuItem>()
            {
                ContextHelper.CreateCopyButton((object sender, RoutedEventArgs e) =>
                {
                    Save();

                    ContextMenu context = (sender as MenuItem).Parent as ContextMenu;
                    MetroTabItem target = context.PlacementTarget as MetroTabItem;
                    ClipboardManager.SetObject(Universal_ItemList.ReturnType.Character, target.DataContext);
                }),
                ContextHelper.CreateDuplicateButton((object sender, RoutedEventArgs e) =>
                {
                    Save();

                    ContextMenu context = (sender as MenuItem).Parent as ContextMenu;
                    MetroTabItem target = context.PlacementTarget as MetroTabItem;
                    var cloned          = (target.DataContext as NPCCharacter).Clone();

                    MainWindow.CurrentProject.data.characters.Add(cloned);
                    MetroTabItem ti = CreateTab(cloned);
                    MainWindow.Instance.characterTabSelect.Items.Add(ti);
                }),
                ContextHelper.CreatePasteButton((object sender, RoutedEventArgs e) =>
                {
                    if (ClipboardManager.TryGetObject(ClipboardManager.CharacterFormat, out var obj) && !(obj is null) && obj is NPCCharacter cloned)
                    {
                        MainWindow.CurrentProject.data.characters.Add(cloned);
                        MetroTabItem ti = CreateTab(cloned);
                        MainWindow.Instance.characterTabSelect.Items.Add(ti);
                    }
                })
            };

            foreach (var cmenuItem in cmenuItems)
            {
                cmenu.Items.Add(cmenuItem);
            }

            tabItem.ContextMenu = cmenu;

            return(tabItem);
        }