This is a migrated thread and some comments may be shown as answers.

Binding RadRichTextBox in WPF-MVVM to a viewmodel property

5 Answers 1241 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 29 Feb 2016, 02:15 PM

Hello All,

      After hours of digging through the forums and help pages and examples, I have NOT found a complete example of how to bind a RadRichTextBox to a viewmodel property.  I would like to see a complete example , all the XAML and all the code in the viewmodel to setup the binding.  I think it should be fairly easy , all I want to do is display some simple text from my database (it's a property in the viewmodel) in a RadRichTextBox...  I'm sure I'll have to use a TxtFormatProvider for the control, but I can't seem to find a complete example of how to do this to bind to a viewmodel property.  The viewmodel property gets set on the viewmodels object instantiation .  I have looked at the Developer Focused Examples and found the data binding example , but that's not a View-View Model example.  

Thanks in advance,

Kevin Orcutt
Senior Software Engineer

Wurth Electronics ICS, Inc.
7496 Webster St., Dayton, OH 45414
Tel: 937.415.7700
Toll Free: 877.690.2207
Fax: 937.415.7710
Email: kevin.orcutt@we-ics.com
http://www.we-ics.com
 

5 Answers, 1 is accepted

Sort by
0
Vitalij
Top achievements
Rank 1
answered on 01 Mar 2016, 05:07 PM

Given I understand your question correctly, DataBinding example is indeed exactly what you are looking for. View is MainPage.xaml and ViewModel is ExampleDataContext.cs. All you need to tweak is how to load your document from database and set it to XamlData property of ViewModel.

For all that to work, it is needed to store your generated document in xaml format in the database and not as plain text. Otherwise you would loose all text formatting when rebinding.

If you are storing and saving plain text then maybe RadRichTextBox is not really what you need for display and edit your data.

 

Best regards,

Vitalij

0
Tanya
Telerik team
answered on 02 Mar 2016, 05:10 PM
Hi Kevin and Vitalij,

As Vitalij said, the Data Binding example demonstrates how a property from a view model could be bound to RadRichTextBox. The Document property is not a dependency property and in order to use it in a MVVM scenario, you will need to use one of the DataProviders. You could choose a data provider depending on the format you are planning to use for storing the document.

Hope this makes things clear.

Regards,
Tanya
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Martin
Top achievements
Rank 1
Veteran
answered on 29 Oct 2020, 12:54 PM

I tried to create the view from the example with the code:

 

<telerik:XamlDataProvider RichTextBox="{Binding ElementName=radRichTextBox}" Xaml="{Binding Path=XamlData, Mode=TwoWay}" />
    <telerik:RadRichTextBox Name="radRichTextBox" Margin="2" Grid.Column="1" />

0
Martin
Top achievements
Rank 1
Veteran
answered on 29 Oct 2020, 12:56 PM

I tried to create the view from the example with the code:

<telerik:XamlDataProvider RichTextBox="{Binding ElementName=radRichTextBox}" Xaml="{Binding Path=XamlData, Mode=TwoWay}" />
    <telerik:RadRichTextBox Name="radRichTextBox" Margin="2" Grid.Column="1" />

But it crashes and throws the error `formatProvider` cannot be null.

Any ideas why?

0
Tanya
Telerik team
answered on 03 Nov 2020, 10:18 AM

Hi Martin,

RadRichTextBox uses MEF to load its required types. In some specific cases, the MEF might fail to load them and such an exception might be thrown. Can you try to register manually them? You can find a list of all the types loaded by MEF in the control's documentation: https://docs.telerik.com/devtools/wpf/controls/radrichtextbox/mef#explicitly-set-the-necessary-types

Hope this is helpful.

Regards,
Tanya
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
RichTextBox
Asked by
Kevin
Top achievements
Rank 1
Answers by
Vitalij
Top achievements
Rank 1
Tanya
Telerik team
Martin
Top achievements
Rank 1
Veteran
Share this question
or