English 中文(简体)
WPF textbox derived control override drag&drop OnPreviewDrop (C sharp)
原标题:

I have been working on my custom control derived from the TextBox and I have encountered a problem I can not solve right now. Brief description of the problem: my textbox contains plain text which contains tags which I want to keep consistent - so far I have overriden text selection so they can be selected only as a whole tag etc. Right now I have moved to processing of drag&drop. If any text is dropped on the textfield and it is dropped on the tag, I want the insertion to be moved before or after the tag. The actual problem is with setting of the e.Handled=true. If I set it to true, it almost works - the text is inserted via my routine, but it is not removed from the source. If I set it to false, after executing my method the original textbox s insertion method is run. Is there any way to alter event routing? Or am I approaching this wrong from the start?

Code of my method: protected override void OnPreviewDragEnter(DragEventArgs e) { base.OnPreviewDragEnter(e); e.Handled = true; // let us draw our very own caret... }

    protected override void OnPreviewDrop(DragEventArgs e)
    {
        base.OnPreviewDrop(e);

        fieldsReady = false;
        int selStart = this.SelectionStart;
        int selLength = this.SelectionLength;

        string droppedData = (string)e.Data.GetData(DataFormats.StringFormat);



        // where to insert
        Point whereDropped = e.GetPosition(this);
        int droppedIndex = GetCharacterIndexFromPoint(whereDropped, true);
        if (droppedIndex == this.Text.Length - 1)
        {
            double c = GetRectFromCharacterIndex(droppedIndex).X;
            if (whereDropped.X > c)
                droppedIndex++;
        }


        // only if the source was us, do this:
        if (this.SelectionLength > 0) // this means that we are dragging from our textbox!
        {

            // was there any selection? if so, remove it!
            this.Text = this.Text.Substring(0, selStart) + this.Text.Substring(selStart + selLength);
            e.Handled = true;

            // 2DO!! alter the indices depending on the removed selection

            // insertion
            this.Text = this.Text.Substring(0, droppedIndex) + droppedData + this.Text.Substring(droppedIndex);

        } 

     }
问题回答

Setting e.Effects in your OnPreviewDrop handler may yield results. My experience was that text was removed from the source. Setting e.Effects = DragDropEffects.Copy leaves my source text alone, which is my desired behavior. Perhaps DragDropEffects.Move would help you when setting e.Handled = true.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签