2

I am working with Silverlight 4 and trying to put my test apps multilingual but I am having some trouble when I arrive to the "RichTextBox" control. I am able to bind it properly by doing back-code (c#), but when trying using the "DataContext" attributes I am not able to load it at all.

I have created a FormatConverter that return a Block (paragraph) for testing and my code where I have my RichTextBox looks like:

   <RichTextBox x:Name="rtaTest" BorderThickness="0" IsReadOnly="True" UseLayoutRounding="True" 
DataContext="{Binding Source={StaticResource Localization}, Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}">
    </RichTextBox>

I am wondering if there is a way of binding a RichTextBox from the XAML.

Todd Main
  • 28,951
  • 11
  • 82
  • 146
Nordes
  • 2,493
  • 2
  • 21
  • 30
  • I'm not quite seeing what you goal is to assign to the DataContext – AnthonyWJones Feb 16 '10 at 14:19
  • It is because I want to put in my rich text area different localization (french/english/etc.) but I want to bind my blocks when I load the page. Sorry if i'm not clear... my english is not my first language. – Nordes Mar 10 '10 at 11:08

4 Answers4

5

Run seems to support databinding in SL4, as in:

<RichTextBox>
  <Paragraph>
    <Run Text="{Binding Path=LineFormatted}" />
  </Paragraph>
</RichTextBox>
Jay Borseth
  • 1,894
  • 20
  • 29
1

I think you may be a little confused about the used of the DataContext. You might for example have some Rich text where some children of one or more InlineUIContainer elements may retrieve their text from a property of some object. You would assign the object to the DataContext.

Whilst I'm not quite sure what you were expecting to achieve but I suspect that what you really need is for your converter to actually return a BlocksCollection (even if it just contains the single Block you were originaly returning) and then to bind as:-

<RichTextArea x:Name="rtaTest" BorderThickness="0" IsReadOnly="True"
 UseLayoutRounding="True"
 Blocks="{Binding Source={StaticResource Localization},
   Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}" />
AnthonyWJones
  • 187,081
  • 35
  • 232
  • 306
1

This FillFromXml is a WPF thing? Don't see it in Silverlight.

user298145
  • 43
  • 4
0

Blocks cannot be set, they can only be fetched. One way to set the blocks for a RichTextArea is

public static void UpdateRichTextArea(RichTextArea area, string xmlText)
{
    if (area == null)
    return;

    area.Blocks.FillFromXml(xmlText, true);
}
stealthyninja
  • 10,343
  • 11
  • 51
  • 59
Rahul N
  • 11
  • 1