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

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

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

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!

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

这不是我的心血来潮,对我来说,用这种方式创建内容真的很容易。 (我使用 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=""/image/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>";

关于c# - 使用 XAML 属性在 RichTextBox 中插入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19037013/

相关文章:

UWP 控件的 XAML 命名空间标识符

c# - 奇怪的 C# 弱引用行为

c# - Convert.ChangeType() 对字符串值有意义吗?

c# - 两个角度的弧度差?

wpf - 来自 Enum 的 MVVM 绑定(bind)

c# - WPF 可检查菜单项中缺少复选标记

c# - 切换按钮两种方式绑定(bind)不起作用(通用 Windows 平台)

c# - LINQ 查询到 Group 并获得 Sum

c# - 当 Item 改变时通知 ObservableCollection

c# - Windows Phone 8 中的设备方向