コード例 #1
0
        /// <summary>
        /// Dropイベント
        /// </summary>
        /// <param name="args"></param>
        private void Description_DragDrop(System.Windows.DragEventArgs args)
        {
            if (args.Data.GetDataPresent(typeof(ContentModel)))
            {
                // Drop item
                var data = args.Data.GetData(typeof(ContentModel)) as ContentModel;

                // EventArgsからDrop先のFrameworkElementを取得
                var feDest = args.Source as System.Windows.Controls.ListBox;
                // EventArgsからDrop先のバインドオブジェクトを取得
                var bindDest = BindingOperations.GetBinding(
                    (args.Source as System.Windows.Controls.ListBox), System.Windows.Controls.ListBox.ItemsSourceProperty).Path.Path;

                // Drop元のバインドオブジェクトを取得
                var dragFromTheme = ThemeList.Any(item => item.ContentList.Contains(data));
                var dragFromFree  = ContentFreeList.Contains(data);

                // Drop元とDrop先のチェック
                if (bindDest == nameof(ThemeList))
                {
                    if (dragFromFree)
                    {
                        Point position = args.GetPosition(args.OriginalSource as IInputElement);
                        //System.Windows.Media.VisualTreeHelper.HitTest(
                        //    this
                        //                         , null
                        //                         , new System.Windows.Media.HitTestResultCallback(OnHitTestResultCallback)
                        //                         , new System.Windows.Media.PointHitTestParameters(position));
                        // Drop Index
                        //var targetContainer = GuiUtils.GetTemplatedRootElement(args.OriginalSource as FrameworkElement);
                        //var targetContainer2 = GuiUtils.FindAncestor<System.Windows.Controls.ListBox>(args.OriginalSource as FrameworkElement);
                        var index = ThemeList.IndexOf(DropTarget);
                        index = index < 0 ? feDest.Items.Count - 1 : index;
                        //var datas =
                        ThemeList[index].ContentList.Add(data);
                        ContentFreeList.Remove(data);
                    }
                    else if (dragFromTheme)
                    {
                        return;
                    }
                }
                else if (bindDest == nameof(ContentFreeList))
                {
                    if (dragFromFree)
                    {
                        // Drop Index
                        var targetContainer = GuiUtils.GetTemplatedRootElement(args.OriginalSource as FrameworkElement);
                        var index           = feDest.ItemContainerGenerator.IndexFromContainer(targetContainer);
                        index = index < 0 ? feDest.Items.Count - 1 : index;
                        // Collectionに反映
                        var ocDest = this[bindDest] as ObservableCollection <ContentModel>;
                        ocDest.Move(feDest.SelectedIndex, index);
                    }
                    else if (dragFromTheme)
                    {
                        // Drop Index
                        var index = ThemeList
                                    .Select((thm, i) => new { Theme = thm, Index = i })
                                    .Where(item => item.Theme.ContentList.Contains(data))
                                    .Select(item => item.Index).FirstOrDefault();

                        // Collectionに反映
                        ContentFreeList.Add(data);
                        ThemeList[index].ContentList.Remove(data);
                    }
                }
                return;
            }
            if (args.Data.GetDataPresent(typeof(ThemeModel)))
            {
                return;
            }
        }