【问题标题】:Insert Image in RichTextBox with XAML property使用 XAML 属性在 RichTextBox 中插入图像
【发布时间】:2013-09-26 19:29:14
【问题描述】:

我想在 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;

我有错误。

我读到了here

这是因为 XAML 解析器不知道如何解析 段落、下划线等元素。为了解决这个问题, 具有实际内容的段落必须包含在 Section 中 定义命名空间的元素,以便元素可以 解决了!

但是&lt;Section xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""&gt; 没有帮助。

这不是我的心血来潮,对我来说,以这种方式创建内容真的很容易。 (我使用 StringFormat、Replace 等)。

可能是什么问题?

提前致谢!

更新

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

异常"System.Windows.Markup.XamlParseException" 行:

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

【问题讨论】:

    标签: c# wpf xaml windows-phone-7 windows-phone-8


    【解决方案1】:

    无法使用 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>";
    

    【讨论】:

    • 感谢您的回答!我检查了链接。但是使用图像它不起作用,请检查更新我的帖子。
    • 替换 i.stack.imgur.com/m0UAA.jpg?s=32&amp;g=1"" /> 。 Xaml 不允许直接使用“&”。所以你必须使用 &
    猜你喜欢
    • 2013-06-19
    • 2022-08-15
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    相关资源
    最近更新 更多