Пример #1
0
        /// <summary>
        /// ファイル一覧にドラッグされた時のイベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected virtual void ListViewDrop(object sender, DragEventArgs e)
        {
            IntPtr            drophelpPtr;
            IDropTargetHelper dropHelper;

            ShellHelper.GetIDropTargetHelper(out drophelpPtr, out dropHelper);
            if (dropHelper != null)
            {
                POINT p = new POINT();
                p.x = (int)e.GetPosition(this).X;
                p.y = (int)e.GetPosition(this).Y;
                dropHelper.Drop((System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, p, e.Effects);
            }
            FileListDataGrid dg = sender as FileListDataGrid;

            if (dg == null)
            {
                return;
            }

            if (!e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                dg.DragMouseButton = DragDropKeyStates.None;
                return;
            }

            string[] files = e.Data.GetData(DataFormats.FileDrop) as string[];
            if (files == null)
            {
                dg.DragMouseButton = DragDropKeyStates.None;
                return;
            }

            string itemprefix = files[0].Substring(0, 2).ToUpper();
            string myprefix   = this.pathUI.Path.Substring(0, 2).ToUpper();

            if (dg.DragMouseButton == DragDropKeyStates.LeftMouseButton)
            {
                if (itemprefix.Equals(@"\\") || myprefix.Equals(@"\\"))
                {
                    this.FileCopy(files);
                }
                else
                {
                    if (itemprefix.Equals(myprefix))
                    {
                        this.FileMove(files);
                    }
                    else
                    {
                        this.FileCopy(files);
                    }
                }
            }
            else if (dg.DragMouseButton == DragDropKeyStates.RightMouseButton)
            {
                #region
                ContextMenu cm = new ContextMenu();
                MenuItem    mi = new MenuItem();
                mi.Header = "Copy";
                mi.Click += (o, arg) =>
                {
                    cm.IsOpen = false;
                    this.FileCopy(files);
                };
                cm.Items.Add(mi);

                mi        = new MenuItem();
                mi.Header = "Move";
                mi.Click += (o, arg) =>
                {
                    cm.IsOpen = false;
                    this.FileMove(files);
                };
                cm.Items.Add(mi);
                cm.IsOpen = true;
                #endregion
            }
        }
Пример #2
0
        protected virtual DataGrid CreateDataGrid()
        {
            DataGrid dg = new FileListDataGrid();

            dg.CanUserAddRows      = false;
            dg.AutoGenerateColumns = false;
            dg.IsReadOnly          = true;
            dg.SelectionMode       = DataGridSelectionMode.Extended;
            dg.SelectionUnit       = DataGridSelectionUnit.FullRow;
            dg.GridLinesVisibility = DataGridGridLinesVisibility.None;
            dg.MouseDoubleClick   += DataGridDoubleClick;
            //データグリッドのSelectionChangedがbubble upしてタブのSlectionChangedがfireされないようHandled=trueにする
            dg.SelectionChanged += (o, e) =>
            {
                e.Handled = true;
            };

            //なんかデータグリッドでなにも選択してない時に、キーイベントをどうやっても拾えなかった。
            //頭来たからWndProcで拾うようにした
            //dg.KeyDown += DataGridKeyDown;

            //コンテキストはWin32APIを使う
            //dg.ContextMenu = this.CreateContextMenu();
            dg.MouseRightButtonUp += DataGridMouseRightButtonUp;

            //DataGridTemplateColumn tmpc = (DataGridTemplateColumn)this.Resources["IconColumn"];
            //tmpc.Width = 30;
            //dg.Columns.Add(tmpc);
            DataTemplate           dtmpl = (DataTemplate)this.Resources["Dtmpl"];
            DataGridTemplateColumn tmpc  = new DataGridTemplateColumn();

            tmpc.CellTemplate = dtmpl;
            tmpc.Width        = 20;
            tmpc.MaxWidth     = 20;
            dg.Columns.Add(tmpc);


            DataGridTextColumn tc = new DataGridTextColumn();

            tc.Width = 180;
            Binding bin = new Binding("Name");

            bin.Mode   = BindingMode.OneWay;
            tc.Header  = "Name";
            tc.Binding = bin;
            dg.Columns.Add(tc);

            tc               = new DataGridTextColumn();
            tc.Width         = 80;
            bin              = new Binding("LastWriteTime");
            bin.Mode         = BindingMode.OneWay;
            bin.StringFormat = "yyyy/MM/dd hh:mm:ss";
            tc.Header        = "Modified";
            tc.Binding       = bin;
            dg.Columns.Add(tc);

            tc            = new DataGridTextColumn();
            tc.Width      = 80;
            bin           = new Binding("Length");
            bin.Mode      = BindingMode.OneWay;
            bin.Converter = (SizeConverter)this.Resources["Sconv"];
            tc.Header     = "Size";
            tc.Binding    = bin;
            Style styl = new System.Windows.Style();

            styl.TargetType = typeof(TextBlock);
            Setter ster = new Setter();

            ster.Property = TextBlock.HorizontalAlignmentProperty;
            ster.Value    = System.Windows.HorizontalAlignment.Right;
            styl.Setters.Add(ster);
            tc.ElementStyle = styl;
            dg.Columns.Add(tc);

            dg.AllowDrop = true;
            dg.Drop     += this.ListViewDrop;

            IntPtr            drophelpPtr;
            IDropTargetHelper dropHelper;

            ShellHelper.GetIDropTargetHelper(out drophelpPtr, out dropHelper);
            dg.DragEnter += (o, e) =>
            {
                if (dropHelper != null)
                {
                    //マネージオブジェクトのIntPtrを取得する方法
                    //でもblittableでないとか言って怒られたorz
                    //DragEnterの第2引数の型をIntPtrから変更した
                    //GCHandle gch = GCHandle.Alloc(e.Data, GCHandleType.Pinned);
                    //IntPtr p = gch.AddrOfPinnedObject();
                    //gch.Free();
                    POINT p = new POINT();
                    p.x = (int)e.GetPosition(this).X;
                    p.y = (int)e.GetPosition(this).Y;
                    dropHelper.DragEnter(this.MyHandle, (System.Runtime.InteropServices.ComTypes.IDataObject)e.Data, ref p, e.Effects);
                }
            };

            dg.DragOver += (o, e) =>
            {
                if (dropHelper != null)
                {
                    POINT p = new POINT();
                    p.x = (int)e.GetPosition(this).X;
                    p.y = (int)e.GetPosition(this).Y;
                    dropHelper.DragOver(p, e.Effects);
                }
            };

            dg.DragLeave += (o, e) =>
            {
                if (dropHelper != null)
                {
                    dropHelper.DragLeave();
                }
            };

            //todo
            //列と列の間はBorderの様なので、列と列の間をクリックすると変な動作してカッコ悪い。
            //あと、選択を解除した後に現在のセル?なのか黒い四角が残るのがかっこ悪い。
            dg.MouseLeftButtonUp += (o, e) =>
            {
                if (!(((DataGrid)o).InputHitTest(e.GetPosition(dg)) is TextBlock))
                {
                    dg.SelectedItem = null;
                }
            };

            return(dg);
        }