簡體   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