简体   繁体   中英

Insert Image in RichTextBox with XAML property

I want insert a image into the RichTextBox control in WP7/8.

I can do it in XAML and it work fine.

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

I can do it in code C#:

        Image MyImage = new Image();
        MyImage.Source = new BitmapImage(new Uri("/ApplicationIcon.png", UriKind.RelativeOrAbsolute));
        MyImage.Height = 50;
        MyImage.Width = 50;
        InlineUIContainer MyUI = new InlineUIContainer();
        MyUI.Child = MyImage;

        Paragraph myParagraph = new Paragraph();
        myRichTextBox.Blocks.Add(myParagraph);

        myParagraph.Inlines.Add(MyUI);

But I can't do this way.

        string xaml =
                @"<Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">
                    <Paragraph>                   
                        <InlineUIContainer>
                             <Image Source=""/ApplicationIcon.png""/>
                        </InlineUIContainer>
                    </Paragraph>                                 
                </Section>";
        myRichTextBox.Xaml = xaml;

I have the error.

I read about it here :

That is happening because the XAML parser does not know how to resolve the Paragraph, Underline, etc. elements. In order to fix this, the paragraphs with the actual content must be wrapped in a Section element that defines the namespace so that the elements can be resolved!

But <Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> don't help.

This is not my whim, for me really easy to create content this way. (I use StringFormat, Replace, etc).

In what could be the problem?

Thanks in advance!

UPDATE

From this article :

public MainPage()
{
    InitializeComponent();
    ListBox list = new ListBox();
    list.ItemTemplate = this.CreateDataTemplate(); 
    list.ItemsSource = new List<string>{"first","second","third","forth"};
    ContentPanel.Children.Add(list);
}

private DataTemplate CreateDataTemplate()
{
      string xaml = @"<DataTemplate
                            xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                            xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
                            <Grid>            
                                <RichTextBox IsReadOnly=""True"">
                                    <Paragraph>                                      
                                        <InlineUIContainer> 
                                                <Image  Source=""http://i.stack.imgur.com/m0UAA.jpg?s=32&g=1"" /> 
                                        </InlineUIContainer>
                                    </Paragraph>
                                </RichTextBox>            
                            </Grid>        
                            </DataTemplate>";
       DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);  
       return dt;
   }

Exception "System.Windows.Markup.XamlParseException" in line:

 DataTemplate dt = (DataTemplate)XamlReader.Load(xaml);  

It is not possible to assign image using Xaml Property of RichTextBox. Refer the below link regarding to elements can be included with the Xaml Property.

http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.xaml(v=vs.95).aspx

If you want to design it dynamically using a list, you can create a DataTemplate Dynamically as given in below link.

http://www.geekchamp.com/tips/wp7-dynamically-generating-datatemplate-in-code

Your Template can be like this:

      @"<DataTemplate
    xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
    xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml"">
    <Grid>            
        <RichTextBox><Paragraph><InlineUIContainer> <Image Height='50' Width='50' Source='/RichTextBoxBinding;component/Desert.jpg' /> </InlineUIContainer></Paragraph></RichTextBox>            
    </Grid>        
    </DataTemplate>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM