views:

2057

answers:

4

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.

A: 

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
A: 

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);
    }`
Rahul N
A: 

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

A: 

Run seems to support databinding in SL4, as in:

<RichTextBox>
  <Paragraph>
    <Run Text="{Binding Path=LineFormatted}" />
  </Paragraph>
</RichTextBox>