繁体   English   中英

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

[英]Insert Image in RichTextBox with XAML property

我想将图像插入WP7 / 8中的RichTextBox控件中。

我可以在XAML中做到这一点,并且工作正常。

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

我可以在代码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);

但是我不能这样。

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

我有错误。

在这里读到它:

发生这种情况是因为XAML解析器不知道如何解析Paragraph,Underline等元素。 为了解决此问题,必须将具有实际内容的段落包装在用于定义名称空间的Section元素中,以便可以解析这些元素!

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

这不是我的想法,对我而言,以这种方式创建内容真的很容易。 (我使用StringFormat,Replace等)。

可能是什么问题?

提前致谢!

UPDATE

这篇文章

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;
   }

行中的异常"System.Windows.Markup.XamlParseException"

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

无法使用RichTextBox的Xaml属性分配图像。 请参阅以下有关Xaml属性可以包含的元素的链接。

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

如果要使用列表动态设计它,则可以按以下链接中的动态创建DataTemplate。

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

您的模板可以像这样:

      @"<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