Igor Meszaros
Igor Meszaros

Reputation: 2127

Richtextbox image with binding not working

Goto Edited

Since the Xaml property of the RichTextbox is not a dependency property I have created a customized RichTextbox where I can interact with its xaml property:

<local:RichTextUserControl RtfXaml="{Binding Path=Text, Converter={StaticResource RichTextBoxContentConverter}}" />

and I am Binding the following text to the xaml property and it is working fine:

<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">
   <Paragraph FontSize=\"20\" FontFamily=\"Segoe WP\" Foreground=\"#FFFFFFFF\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\">
      <Run Text=\"Some text without formatting\" />
      <Italic>Some italic text</Italic> 
      <Underline>I am UnderLined</Underline>
   </Paragraph>
</Section>

I am binding with it trough a converter, where I search for smiley characters (for example :) ;) :D so on...) and replacing them with images, and if I insert the following code somewhere in-between the paragraph text it crashes:

<InlineUIContainer>
    <Image Source="ApplicationIcon.png"/>
</InlineUIContainer>

(It is only exception when it is with binding)

Edited:

So I found out this was a bad aproach, and I started to implement it this way:

<RichTextBox Tag="{Binding Path=MessageText}" TextWrapping="Wrap" Loaded="loaded"/>

        private void loaded(object sender, RoutedEventArgs e)
        {
                var richTextBox= sender as RichTextBox;
                Object o = XamlReader.Load(string.Format(XamlTemplate, richTextBox.Tag.ToString()));
                var section = o as Section;
                if (section  != null)
                {
                    richTextBox.Blocks.Clear();
                    var tempBlocks = section.Blocks.ToList();
                    section.Blocks.Clear();
                    foreach (Block block in tempBlocks)
                        richTextBox.Blocks.Add(block);
        }

private const string XamlTemplate = "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph FontSize=\"20\" FontFamily=\"Segoe WP\" Foreground=\"#FFFFFFFF\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\"><Run Text=\"{0}\" /><Image Source=\"ApplicationIcon.png\" Width=\"15\" Height=\"15\"/></InlineUIContainer> </Paragraph></Section>";

So I am parsing the Xaml on the textboxs loaded event, with text and string. XamlTemplate is a hardcoded text with a smiley template.

My smiley is working this way, but when I scroll down in my listbox where there are numbers of these Richtextboxes, the scrolling starts jumping, and it is really annoying.

But when I change the listbox items to a fixed size it is working fine, but I need to change the sizes for the items dynamically, any ideas on that?

Upvotes: 1

Views: 1085

Answers (2)

Swapnika
Swapnika

Reputation: 480

Try with this code.

    private Regex rxForbidden = new Regex(@"[<;]", RegexOptions.IgnoreCase);
    string[] stringArray;

    private void richTextbox_Loaded(object sender, RoutedEventArgs e)
    {
         var richTextBox= sender as RichTextBox;
         string t = richTextBox.Tag.ToString();
         Paragraph myParagraph = new Paragraph();

         if (rxForbidden.IsMatch(t))
         {
             s = rxForbidden.Split(t);
             richTextBox.Blocks.Add(myParagraph);

             for (int i = 0; i < s.Count(); i++)
             {

                 if (s[i] != null && s[i] != "")
                 {
                     Run txt = new Run();
                     txt.Text = s[i];
                     myParagraph.Inlines.Add(txt);

                 }
                 else
                 {
                     Image MyImage = new Image();
                     MyImage.Source = new BitmapImage(new Uri("/RichTextBoxText;component/smiley_72x72.png", UriKind.Relative));
                     MyImage.Height = 30;
                     MyImage.Width = 30;
                     InlineUIContainer MyUI = new InlineUIContainer();
                     MyUI.Child = MyImage;
                     myParagraph.Inlines.Add(MyUI);
                 }
             }
             richTextBox.Blocks.Add(myParagraph);
         }

Upvotes: 1

jv42
jv42

Reputation: 8583

I think your image URI is invalid. It should be something like:

<InlineUIContainer>
    <Image Source="/YourApplication;component/ApplicationIcon.png"/>
</InlineUIContainer>

Upvotes: 0

Related Questions