Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bind data with the rich text box with MVVM pattern?

Tags:

c#

mvvm

wpf

How to bind data dynamically with the document property of the rich textbox. I am using MVVM in Wpf with c#?

EDIT:

I tried with this example in "codeproject.com/KB/WPF/BindableWPFRichTextBox.aspx"; but i can't understand what is happening in that example. I am very new to WPF and MVVM.

It's throwing error in the line

try { 
  var stream = new MemoryStream(Encoding.UTF8.GetBytes(GetDocumentXaml(richTextBox)));
  var doc = (FlowDocument)XamlReader.Load(stream); 
  // Set the document 
  richTextBox.Document = doc; 
} 
catch (Exception) { richTextBox.Document = new FlowDocument(); }

the error is like "Data at the root level is invalid. Line 1, position 1." i am giving value like "Sample Text"

I found the xaml text should be like

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" xmlns="schemas.microsoft.com/winfx/2006/xaml/… generated by app back-end</Paragraph>
</FlowDocument>" But how to get this text?
like image 958
Tanya Avatar asked Jan 16 '12 13:01

Tanya


People also ask

How do I bind Mvvm?

MVVM – WPF Data Bindings For data binding you need to have a view or set of UI elements constructed, and then you need some other object that the bindings are going to point to. The UI elements in a view are bound to the properties which are exposed by the ViewModel.

When should we use RichTextBox?

A RichTextBox is a better choice when it is necessary for the user to edit formatted text, images, tables, or other rich content. For example, editing a document, article, or blog that requires formatting, images, etc is best accomplished using a RichTextBox.

What is RichTextBox?

In C#, RichTextBox control is a textbox which gives you rich text editing controls and advanced formatting features also includes a loading rich text format (RTF) files. Or in other words, RichTextBox controls allows you to display or edit flow content, including paragraphs, images, tables, etc.


1 Answers

I hope I interpret your question correctly: I assume you are binding to a normal string (sample text) with the RichTextBox you got from codeproject. This will not work, 'cause the Document you have to bind is a FlowDocument and it has a specific format. If you assign a string you will get the error "data invalid" when it tries to create a FlowDocument from the string

Here's a link on how to create a FlowDocument via XAML or via CodeBehind. http://msdn.microsoft.com/en-us/library/aa970909.aspx

Then the converter comes into play: Out of the string representation it creates a real FlowDocument.

So, if you want to display your sample text bind to a string in the VM like this:

<FlowDocument PagePadding=\"5,0,5,0\" AllowDrop=\"True\" " 
                + "xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">"
                + "<Paragraph>Your sample text</Paragraph></FlowDocument>"
like image 158
SvenG Avatar answered Oct 27 '22 00:10

SvenG