Пример #1
0
        private void ListBox_Drop <TItem>(Object sender, DragEventArgs e) where TItem : DragableListModelBase
        {
            ListBox listbox = (ListBox)sender;

            Point         pos    = e.GetPosition(listbox);
            HitTestResult result = VisualTreeHelper.HitTest(listbox, pos);

            if (result == null)
            {
                return;
            }

            //查詢目標資料
            var listBoxItem = FindVisualParent <ListBoxItem>(result.VisualHit);

            if (listBoxItem == null)
            {
                return;
            }

            //查詢後設資料

            object target = listBoxItem.Content;

            DragableListModelBase source = null;

            if (Type.GetType(e.Data.GetFormats()[0]).BaseType == typeof(DragableListModelBase))
            {
                source = e.Data.GetData(e.Data.GetFormats()[0]) as DragableListModelBase;
            }
            else
            {
                return;
            }

            ListBoxViewModel <TItem> viewModel = (ListBoxViewModel <TItem>)listbox.DataContext;

            if (source.GetType() == target.GetType())
            {
                viewModel.LastSelectModel?.ClearIndicator();
                MoveItem <ObservableCollection <TItem>, TItem>(viewModel.ModelCollection, source, target);
            }
            //command move to other tab
            else if (source.GetType() == typeof(StrItemModel) && target.GetType() == typeof(TabItemModel))
            {
                if (tabListBoxViewModel.LastSelectModel != null)
                {
                    tabListBoxViewModel.LastSelectModel.MoveInIndicator = false;
                }
                List <StrItemModel> moveCommandsList = new List <StrItemModel>();
                moveCommandsList.Add((StrItemModel)ListBox_Str.SelectedItem);
                MoveItemToTab((TabItemModel)ListBox_Tab.SelectedItem, (TabItemModel)target, moveCommandsList);

                TabItemModel selectedItem = (TabItemModel)this.ListBox_Tab.SelectedItem;
                if (selectedItem != null)
                {
                    TabStrRefresh(selectedItem.Data.StrList);
                    //selectedItem.Checked = true;
                }
            }
            //tab move to other string (x)
            else
            {
            }
        }
Пример #2
0
        //FIXME: When command drag to tab and drag back,tab indicator not recovered;
        private void ListBox_DragOver <TItem>(object sender, DragEventArgs e) where TItem : DragableListModelBase
        {
            ListBox listbox = (ListBox)sender;
            ListBoxViewModel <TItem> viewModel = (ListBoxViewModel <TItem>)listbox.DataContext;

            Point pos = e.GetPosition(listbox);

            if (listbox.ActualHeight - pos.Y <= autoScrollThreshold)
            {
                //下捲
                Border       border       = (Border)VisualTreeHelper.GetChild(listbox, 0);
                ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 1);
            }
            else if (pos.Y <= autoScrollThreshold)
            {
                //上捲
                Border       border       = (Border)VisualTreeHelper.GetChild(listbox, 0);
                ScrollViewer scrollViewer = (ScrollViewer)VisualTreeHelper.GetChild(border, 0);
                scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - 1);
            }

            HitTestResult result = VisualTreeHelper.HitTest(listbox, pos);
            ListBoxItem   listBoxItem;

            if (result != null)
            {
                listBoxItem = FindVisualParent <ListBoxItem>(result.VisualHit);
            }
            else
            {
                Console.WriteLine("Clear Indi");
                viewModel.LastSelectModel?.ClearIndicator();
                viewModel.LastSelectModel = null;
                return;
            }

            if (listBoxItem == null)
            {
                Console.WriteLine("Clear Indi2");
                viewModel.LastSelectModel?.ClearIndicator();
                //viewModel.LastSelectModel = null;
                return;
            }

            TItem target = (TItem)listBoxItem.Content;
            DragableListModelBase source; //  = e.Data.GetData(typeof(TItem)) as TItem

            if (Type.GetType(e.Data.GetFormats()[0]).BaseType == typeof(DragableListModelBase))
            {
                source = e.Data.GetData(e.Data.GetFormats()[0]) as DragableListModelBase;
            }
            else
            {
                return;
            }

            if (target == source)
            {
                viewModel.LastSelectModel?.ClearIndicator();
                viewModel.LastSelectModel = null;
                if (source.GetType() == typeof(StrItemModel))
                {
                    if (tabListBoxViewModel.LastSelectModel != null)
                    {
                        tabListBoxViewModel.LastSelectModel.MoveInIndicator = false;
                    }
                }
                return;
            }
            else
            {
                if (source.GetType() == target.GetType())
                {
                    if (target != viewModel.LastSelectModel)
                    {
                        viewModel.LastSelectModel?.ClearIndicator();
                        if (source.GetType() == typeof(StrItemModel))
                        {
                            Console.WriteLine("trig");
                            TabItemModel lastModel = tabListBoxViewModel.LastSelectModel;
                            Console.WriteLine(lastModel);
                            if (lastModel != null)
                            {
                                lastModel.MoveInIndicator = false;
                            }
                        }
                    }
                    viewModel.LastSelectModel = target;

                    int targetIndex = viewModel.ModelCollection.IndexOf(target);
                    int sourceIndex = viewModel.ModelCollection.IndexOf((TItem)source);

                    if (targetIndex > sourceIndex)
                    {
                        target.DragIndicatorDown = true;
                    }
                    else
                    {
                        target.DragIndicatorUp = true;
                    }

                    //if (source.GetType() == typeof(StrItemModel))
                    //{
                    //    if (tabListBoxViewModel.LastSelectModel != null)
                    //    {
                    //        tabListBoxViewModel.LastSelectModel.MoveInIndicator = false;
                    //    }
                    //}
                }
                //command move to other tab
                else if (source.GetType() == typeof(StrItemModel) && target.GetType() == typeof(TabItemModel))
                {
                    strListBoxViewModel.LastSelectModel?.ClearIndicator();
                    strListBoxViewModel.LastSelectModel = (StrItemModel)source;

                    if (tabListBoxViewModel.LastSelectModel != null)
                    {
                        tabListBoxViewModel.LastSelectModel.MoveInIndicator = false;
                    }

                    tabListBoxViewModel.LastSelectModel = (TabItemModel)(DragableListModelBase)target;
                    Console.WriteLine("Move to other tab" + tabListBoxViewModel.LastSelectModel.Data.Name);
                    tabListBoxViewModel.LastSelectModel.MoveInIndicator = true;
                }
                //tab move to other string (x)
                else
                {
                    Console.WriteLine("X");
                    tabListBoxViewModel.LastSelectModel?.ClearIndicator();
                    tabListBoxViewModel.LastSelectModel = (TabItemModel)source;
                    e.Effects = DragDropEffects.None;
                }
            }
        }