RichTextBox How To!  
Author Message
CodeDjinn





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

Hi,

I have searched the net and the documentation that shipped with Beta 2 and have not found any results or information that was able to aid me.  Hopefully there are some RichTextBox experts that could please help me with the following things:

  1. How can I make the RichTextBox single line   (this was possible in Win32, and no, changing the control template to a textbox does not work!  Because then it essentially is a textbox!)
  2. Why is it so difficult to control drag/drop operations in both the textbox and RichTextBox   I would like to control over what is dropped onto the textbox, for instance if images is dropped or texted I would like to be able to
  3. Is it possible when doing a drag/drop operation, I detect my own data type, and then insert an InlineUIContainer exactly where the CaretPosition was when I dropped the data in to the RichTextBox

I know this is a lengthy request, but if I can achieve this, I would be very happy.

Thank you,

Jaco

 



Visual Studio 20085  
 
 
lee d





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

did you try setting AcceptsReturn=false

 
 
CodeDjinn





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

That works until you paste data that contains a line break. Then the whole thing is busted. But because you cannot stop the text from being dropped, I actually have to parse the string by removing the spaces, removing the text that was dropped, and adding the parsed text. And this is really unsatisfactory.


 
 
lee d





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

you can get the caretposition and from that we can add our UIElements. I was able to drag and drop my custom UIElement , I used

richTextBox.CaretPosition.Paragraph.Inlines.Add(uic);



 
 
CodeDjinn





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

Yes but it doesn't add it at the position that the drag event triggers, e.g if you drag text, you can drag it and a lighter cursor appears showing where the text wil be dropped. Dropping a UIElement inserts it where the "real" cursor is.


 
 
lester - MSFT





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

you can use the DataObjectPastingEventHandler to determine the format you want to paste

HTH


 
 
lester - MSFT





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

issue 1 is a bug. Thanks for pointing out  that a newline is pasted even after setting AcceptsReturn=false.
 
 
CodeDjinn





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

Thank you for all you're help so far. So the bug-fix wil probably only be available in the next release of WinFX Or wil there be a hot-fix available

"richTextBox.CaretPosition.Paragraph.Inlines.Add(uic);" is exactly what I did. (even tried InlineContainer(element, textPointer) )

But it seems to insert it at the last position of the cursor, not at the semi-transparent cursor which appears when dragging into the control.


 
 
lester - MSFT





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

this should do the trick.

rtb.AddHandler(DataObject.PastingEvent, new DataObjectPastingEventHandler(PasteEvent), true);

private void PasteEvent(object sender, DataObjectPastingEventArgs e)

{

string [] dataFormats = e.DataObject.GetFormats();

foreach (String str in dataFormats)

{

if (str == DataFormats.Xaml)

{

string data = "<Span xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xml:space=\"preserve\" xml:lang=\"en-us\"><InlineUIContainer><TextBox/></InlineUIContainer></Span>";

e.DataObject.SetData(DataFormats.Xaml, data, true);

}

}

}


 
 
CodeDjinn





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

Thank you very much! It works!

If I could get answers to 2 more questions then I am settled.

1.  I know you said it was a bug, but for now, is there a workaround that I can remove newline characters from text that will be dropped I have tried setting the e.DataObject but it seems the object is frozen for text data.

2.  When adding the XAML like the example you  have just shown me, is it possible to have a Canvas as the top element

Thank you very much for you're time,

Jaco


 
 
lester - MSFT





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

within the span you can have a canvas.. that should work.
 
 
lester - MSFT





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

oops forgot about the first question :) ... you could do the following

private void PasteEvent(object sender, DataObjectPastingEventArgs e)

{

e.FormatToApply = DataFormats.Text;

string d = e.DataObject.GetData(DataFormats.Text,true) as string;

d = d.Replace("\r\n", " ");

e.DataObject.SetData(DataFormats.Text, d, true);

}


 
 
CodeDjinn





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

Thanks again, but I have tried it that way but I get an Exception stating that the object is frozen, so the data cannot be modified. Any idea how to go around that


 
 
CodeDjinn





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

If there is no content in the RichTextBox, the first span takes up the whole line and does not display the Canvas inside, but if there is already at least a character present, the Canvas is added with the right width.  Any idea why   

That said, is there any way to disable the default shortcut keys for enabling formatting in a RichTextBox, e.g Ctrl+b triggers bold, Ctrl+i triggers italics, etc.

Will there be a fix for the error associated with the AcceptsReturn=false property

Thank you very much,

Jaco

 


 
 
lester - MSFT





PostPosted: Windows Presentation Foundation (WPF), RichTextBox How To! Top

If there is no content then the InlineUIContainer would be added as a BlockUIContainer - that explans the whole line behavior.