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?
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.
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.
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.
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>"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With