2

I'm having difficulty adding the inline of specific type InlineUIContainer into the InlineCollection (Content property) of a TextBlock. It appears the .Add() method of InlineCollection doesn't accept this type, however you can clearly set it through XAML without explicitly marking the content as a InlineContainer, as demonstrated in many examples:

http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx

Is it possible to programatically add one of these as in the following?

Target.Inlines.Add(new Run() { Text = "Test" });
Target.Inlines.Add(new InlineUIContainer() { 
Child = new Image() { Source = new BitmapImage(new Uri("http://example.com/someimage.jpg")) } });
Target.Inlines.Add(new Run() { Text = "TestEnd" });

I have a feeling what's going on is that Silverlight is using a value converter to create the runs when specified in XAML as in the example which doesn't use InlineContainer, but I'm not sure where to look to find out.

The specific error I'm getting is as follows:

Cannot add value of type 'System.Windows.Documents.InlineUIContainer' to a 'InlineCollection' in a 'System.Windows.Controls.TextBlock'.
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Sprague
  • 1,610
  • 10
  • 22
  • 2
    Just a note, I'm almost positive this works in WPF although I haven't tried it. I have a feeling this is something not implemented in Silverlight. If you look at the .NET Framework 4 description of InlineUIContainer.Child on MSDN, you can see an example which looks surprisingly similar to what I have, but I can't find the same example on the Silverlight version of the page (in their example, they use a RichTextBox instead of a TextBlock). – Sprague Jan 06 '11 at 20:33
  • 1
    This does work in WPF (http://stackoverflow.com/questions/861409/wpf-making-hyperlinks-clickable). I think we are forced to use RichTextBox in Silverlight. – Jedidja May 25 '11 at 17:17

2 Answers2

3

As pointed out by Jedidja, we need to use RichTextBox to do this in Silverlight.

Sprague
  • 1,610
  • 10
  • 22
  • 1
    So mark his comment down and explain to me what's incorrect about it, please... If you could post an answer, even better. – Sprague Nov 27 '12 at 14:26
  • This was helpful to me. I was porting MUI's BBCode logic to Silverlight, and this was one of the hiccups I encountered along the way. Specifically, a Span object containing a HyperLink object caused an exception when the Span was added to the TextBlock. Replacing the TextBlock with a RichTextBlock (wrapped in a Paragraph, which was necessary) worked perfectly. – Matt Burnell Oct 07 '15 at 00:53
0

You can't Add() Runs directly, but you can add Spans containing Runs.

Interestingly, you can also do this:

textBlock.Inlines.Clear();
textBlock.Inlines.Add(new Span());
textBlock.Inlines[0] = new Run();

Not that it's a good idea to hack around what the framework is actively trying to prevent you from doing.

P.S. If you can't figure out what XAML is doing, inspect the visual tree.

Aleksandr Dubinsky
  • 22,436
  • 15
  • 82
  • 99