Reputation: 2997
I know from MSDN documentation that you cannot export an Image which is part of a RichTextBox using the XAML property. This is fine, I can work around that by relection and looking through the blocks manually.
My question is, if I re-build the XAML manually to include an Image, would the RichTextBox be able to load it from xaml.
I've implemented the reflection and manual XAML export already and it works perfectly without images.
With images it produces this:
<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph TextAlignment="Left" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000" >
<Run Text="Test" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000" />
</Paragraph>
<Paragraph TextAlignment="Left" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000" >
<InlineUIContainer>
<Image Source="./desert.jpg" Height="150" Width="200" />
</InlineUIContainer>
<Run Text="" FontSize="20" FontFamily="Portable User Interface" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" Foreground="#FF000000" />
</Paragraph>
</Section>
Which I feed back into the RTB via the XAML property and breaks! (The exception is useless, just an IllegalArgmentException saying 'Value'.
If you take out just the InlineUIContainer section its fine!
I can't work out if its possibly a problem with the image location being wrong or the RichTextBox just not accepting images apart from in-code.
The only reason why I think it's possibly to specify an image from xaml is because the MSDN documents shows it: http://msdn.microsoft.com/en-us/library/ee681613(VS.95).aspx.
Any ideas?
Ta,
Andy.
Upvotes: 1
Views: 1383
Reputation: 5716
In XAML ./desert.jpg source will not work. Instead use this one
<Image Source="YourNameSpaceBus;component/images/desert.jpg"
Height="150" Width="200" />
Here are two important keywords first one is your namespace ProjectBus
The secondone is fixed "component"
then your imagepath you need to write. Otherwise even if its displayable on designtime sometimes it doesnt work on runtime.
such as
<Image Source="AHBSBus;component/images/mail.png" Stretch="None" Height="23">
</Image>
Hope helps
Upvotes: 0
Reputation: 2997
Well, I've found a way to do it, all-be-it not loading the XAML straight into the RTB using the XAML property.
To load XAML with images into the RTB I've had to revert to loading the XAML into objects first using the XamlReader object, then adding the blocks one by one, as per this code:
// Load up the XAML using the XamlReader
Object o = XamlReader.Load(xamlTb.Text);
if (o is Section)
{
// Make sure its a section and clear out the old stuff in the rtb
Section s = o as Section;
rtb.Blocks.Clear();
// Remove the blocks from the section first as adding them straight away
// to the rtb will throw an exception because they are a child of two controls.
List<Block> tempBlocks = new List<Block>();
foreach (Block block in s.Blocks)
{
tempBlocks.Add(block);
}
s.Blocks.Clear();
// Add them block by block to the RTB
foreach (Block block in tempBlocks)
{
rtb.Blocks.Add(block);
}
}
Not as neat as I would have helped, but I guess the XAML property just doesn't parse InlineUIElements.
Andy.
Upvotes: 0
Reputation: 189437
The Xaml
property on the RichTextBox
does not support InlineUIContainer
either in or out.
One work around I would try first is to use the XamlReader on your xaml instead then add the result to the RichTextBox.Blocks
collection:-
Section section = (Section)XamlReader.Load(yourXaml);
yourRTB.Blocks.Add(section);
Upvotes: 2