I tried the following:
public
partial
class
DocumentPreview : ChildWindow
{
public
DocumentPreview(
object
param)
{
DocumentPreviewModel _viewModel =
new
DocumentPreviewModel(param);
this
.DataContext = _viewModel;
InitializeComponent();
}
...
}
<
telerik:RadRichTextBox
DataContext
=
"{Binding Path=PrintPreviewDocument}"
Height
=
"233"
/>
But i get an empty document.
Is my binding incorrect? how should i bind my richtextbox to a raddocument?
8 Answers, 1 is accepted
RadRichTextBox uses data providers in order to be able to bind its document to strings in different file formats - e.g. HTML, XAML and RTF. You can read more on the way data providers are used in this article.
In the latest 2012.1 326 we have also introduced attached properties, which can be used as follows:
<
telerik:RadRichTextBox
Grid.Row
=
"1"
Name
=
"editor"
telerikHtml:HtmlDataProvider.Source
=
"{Binding PrintPreviewDocument}"
/>
where the telerikHtml namespace declaration is:
xmlns:telerikHtml="clr-namespace:Telerik.Windows.Documents.FormatProviders.Html;assembly=Telerik.Windows.Documents.FormatProviders.Html"
Note that the PrintPreviewDocument should be a string representing a document in HTML format and not a RadDocument.
I hope this helps. Greetings,
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I tried this:
<
telerik:RadRichTextBox
Margin
=
"2"
x:Name
=
"radRichTextBox"
telerikXAML:XamlDataProvider.Source
=
"{Binding PrintPreviewDocument}"
/>
<
telerikXAML:XamlDataProvider
x:Name
=
"xamlDataProvider"
Xaml
=
"{Binding Path=PrintPreviewDocument, Mode=OneTime}"
RichTextBox
=
"{Binding ElementName=radRichTextBox}"
/>
<
telerik:RadRichTextBox
Margin
=
"2"
x:Name
=
"radRichTextBox"
/>
But neither works for me.
What am i doing wrong here?
You can check two things: in order to verify that the Binding works correctly, you can try inserting a TextBox in the place of the RadRichTextBox/DataProvider and binding it to the same property. If text appears in it, then the binding is correct. If it doesn't, you should check the data context, property names, etc.
The second thing is to try to import the value manually, through the use of XamlFormatProvider as shown here and see if it imports without an exception. If it throws one, make sure to check its content and fix the document accordingly. Finally, if that doesn't help, you can prepare us a small demo, containing only your model class, a single page of UI that demonstrates the problem so we can investigate further.
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I have tried the textbox to see what actually comes out of the provider.
What i see is simply a tag indicating that it is the document.
Now from what i can make out the provider is expecting the XAML of the document not the document itself.
So again, i ask you, how do i bind this document to the richtextboxcontrol?
Above i have shown some code which i wanted to use that naturally does not work as it is trying to bind a document rather then the documents XAML. I don't quite understand why you are telling me how to test my code rather then simply telling me what the actual binding is. When we both know the code is wrong. and just an example of what i am trying to achieve.
Any chance of you simply telling me:
What you looking for is:
Xaml="{Binding Path=PrintPreviewDocument.XAML, Mode=OneTime}"
Again, the ".XAML" is just a made up property.
I am slightly confused how i can be the first person ever wanting to do this. After all binding is pretty much how all data is passed to the view in silverlight.
Also, just to clarify, there are no errors in the actual document nor in the code that retrieve it as i can do what i am trying to in teh code behind. The only problem is with the binding code.
I need binding a docx from a VM. How I can do it?
Thanks and sorry for my english,
Jose
@Andrew - To bind a Xaml string you need a XamlDataProvider and a RichTextBox like so:
<
telerik:RadRichTextBox
Width
=
"376"
Height
=
"258"
x:Name
=
"editor"
HorizontalAlignment
=
"Left"
Margin
=
"12,12,0,0"
VerticalAlignment
=
"Top"
/>
<
xamlDataProvider:XamlDataProvider
RichTextBox
=
"{Binding ElementName=editor}"
Xaml
=
"{Binding Xaml}"
/>
public
class
ViewModel
{
public
string
Xaml
{
get
{
return
@
"<t:RadDocument xmlns="
"http://schemas.microsoft.com/winfx/2006/xaml/presentation"
"
xmlns:t=
""
clr-
namespace
:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents
""
xmlns:s=
""
clr-
namespace
:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents
""
version=
""
1.2
""
LayoutMode=
""
Flow
""
LineSpacing=
""
1.15
""
LineSpacingType=
""
Auto
""
ParagraphDefaultSpacingAfter=
""
12
""
ParagraphDefaultSpacingBefore=
""
0
""
SectionDefaultPageSize=
""
816,1056
""
StyleName=
""
defaultDocumentStyle
""
>
<t:Section>
<t:Paragraph>
<t:Span Text=
""
testing
""
/>
</t:Paragraph>
</t:Section>
</t:RadDocument>";
}
}
}
@Jose - you can bind Xaml, Html and Rtf data providers to string properties. Binding to DocX requires that the property must be of type byte []. Other than that, setting up the view model and the data provider is analogical to that for XamlDataProvider.
All the best,
Andrew
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Lets try this again.
I would like to bind a RadDocument to my richtextbox, not a string of HTML or XML an actual RadDocument.
To demonstrate here is a bit of code from my ViewModel.
private
RadDocument _printPreviewDocument;
public
RadDocument PrintPreviewDocument
{
get
{
return
_printPreviewDocument; }
set
{
if
(value != _printPreviewDocument)
{
_printPreviewDocument = value;
OnPropertyChanged(
"PrintPreviewDocument"
);
}
}
}
Perhaps, one simply cannot do that? If that is the case fair enough.
In that case how do i get the XAML from my RadDocument?
If i need to bind to a string (XAML) then how do i read that string from the RadDocument?
And another question why is it that in my code behind i can simply assign a RadDocument to the RichTExtBox.Document? But cannot bind those in the XAML?
Binding RadDocument adds little value to the scenarios in which RadRichTextBox is normally used. As you can obtain a RadDocument instance only by building one in code-behind, it is much simpler to set it explicitly to the Document property of RadRichTextBox instead of binding it. Furthermore, binding the content of the document to strings in different formats provides you with the option to bind the document directly to a database field and only leaves the submission of the changes to you.
If you decide to use XamlDataProvider to bind the content of the rich text box to a string in XAML format, you can use XamlFormatProvider to import/export the document you have prepared like this:
XamlFormatProvider formatProvider =
new
XamlFormatProvider();
string
xamlString = formatProvider.Export(PrintPreviewDocument);
PrintPreviewDocument = formatProvider.Import(xamlString);
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>