简体   繁体   English

使用XAML属性在RichTextBox中插入图像

[英]Insert Image in RichTextBox with XAML property

I want insert a image into the RichTextBox control in WP7/8. 我想将图像插入WP7 / 8中的RichTextBox控件中。

I can do it in XAML and it work fine. 我可以在XAML中做到这一点,并且工作正常。

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

I can do it in code C#: 我可以在代码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. 发生这种情况是因为XAML解析器不知道如何解析Paragraph,Underline等元素。 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! 为了解决此问题,必须将具有实际内容的段落包装在用于定义名称空间的Section元素中,以便可以解析这些元素!

But <Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""> don't help. 但是<Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation"">没有帮助。

This is not my whim, for me really easy to create content this way. 这不是我的想法,对我而言,以这种方式创建内容真的很容易。 (I use StringFormat, Replace, etc). (我使用StringFormat,Replace等)。

In what could be the problem? 可能是什么问题?

Thanks in advance! 提前致谢!

UPDATE 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: 行中的异常"System.Windows.Markup.XamlParseException"

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

It is not possible to assign image using Xaml Property of RichTextBox. 无法使用RichTextBox的Xaml属性分配图像。 Refer the below link regarding to elements can be included with the Xaml Property. 请参阅以下有关Xaml属性可以包含的元素的链接。

http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.xaml(v=vs.95).aspx 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. 如果要使用列表动态设计它,则可以按以下链接中的动态创建DataTemplate。

http://www.geekchamp.com/tips/wp7-dynamically-generating-datatemplate-in-code 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>";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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