Image 绑定无效的Richtextbox图像

Image 绑定无效的Richtextbox图像,image,xaml,windows-phone-7,binding,richtextbox,Image,Xaml,Windows Phone 7,Binding,Richtextbox,转到已编辑的 由于RichTextbox的Xaml属性不是依赖项属性,因此我创建了一个自定义的RichTextbox,可以在其中与它的Xaml属性交互: <local:RichTextUserControl RtfXaml="{Binding Path=Text, Converter={StaticResource RichTextBoxContentConverter}}" /> 我正在将以下文本绑定到xaml属性,它工作正常: <Section xml:space=\

转到已编辑的

由于RichTextbox的Xaml属性不是依赖项属性,因此我创建了一个自定义的RichTextbox,可以在其中与它的Xaml属性交互:

<local:RichTextUserControl RtfXaml="{Binding Path=Text, Converter={StaticResource RichTextBoxContentConverter}}" />

我正在将以下文本绑定到xaml属性,它工作正常:

<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">
   <Paragraph FontSize=\"20\" FontFamily=\"Segoe WP\" Foreground=\"#FFFFFFFF\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\">
      <Run Text=\"Some text without formatting\" />
      <Italic>Some italic text</Italic> 
      <Underline>I am UnderLined</Underline>
   </Paragraph>
</Section>

一些斜体字
我被划了线
我通过转换器与它绑定,在那里我搜索笑脸字符(例如:);):D等等…)并将其替换为图像,如果在段落文本之间插入以下代码,则会崩溃:

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

(只有在绑定时才例外)

已编辑

所以我发现这是一个糟糕的方案,我开始这样实施:

<RichTextBox Tag="{Binding Path=MessageText}" TextWrapping="Wrap" Loaded="loaded"/>

        private void loaded(object sender, RoutedEventArgs e)
        {
                var richTextBox= sender as RichTextBox;
                Object o = XamlReader.Load(string.Format(XamlTemplate, richTextBox.Tag.ToString()));
                var section = o as Section;
                if (section  != null)
                {
                    richTextBox.Blocks.Clear();
                    var tempBlocks = section.Blocks.ToList();
                    section.Blocks.Clear();
                    foreach (Block block in tempBlocks)
                        richTextBox.Blocks.Add(block);
        }

private const string XamlTemplate = "<Section xml:space=\"preserve\" HasTrailingParagraphBreakOnPaste=\"False\" xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"><Paragraph FontSize=\"20\" FontFamily=\"Segoe WP\" Foreground=\"#FFFFFFFF\" FontWeight=\"Normal\" FontStyle=\"Normal\" FontStretch=\"Normal\" TextAlignment=\"Left\"><Run Text=\"{0}\" /><Image Source=\"ApplicationIcon.png\" Width=\"15\" Height=\"15\"/></InlineUIContainer> </Paragraph></Section>";

已加载私有void(对象发送器、路由目标)
{
var richTextBox=发送方为richTextBox;
对象o=XamlReader.Load(string.Format(XamlTemplate,richTextBox.Tag.ToString());
var段=o为段;
if(节!=null)
{
richTextBox.Blocks.Clear();
var tempBlocks=section.Blocks.ToList();
section.Blocks.Clear();
foreach(tempBlocks中的块)
richTextBox.Blocks.Add(block);
}
私有常量字符串XamlTemplate=“”;
因此,我正在用文本和字符串解析textboxs loaded事件上的Xaml。XamlTemplate是一个带有笑脸模板的硬编码文本

我的笑脸是这样工作的,但是当我在我的列表框中向下滚动时,其中有很多RichTextBox,滚动开始跳跃,这真的很烦人


但是,当我将列表框项目更改为固定大小时,它工作正常,但我需要动态更改项目的大小,对此有何想法?

我认为您的图像URI无效。 应该是这样的:

<InlineUIContainer>
    <Image Source="/YourApplication;component/ApplicationIcon.png"/>
</InlineUIContainer>

我认为您的图像URI无效。 应该是这样的:

<InlineUIContainer>
    <Image Source="/YourApplication;component/ApplicationIcon.png"/>
</InlineUIContainer>

尝试使用此代码

    private Regex rxForbidden = new Regex(@"[<;]", RegexOptions.IgnoreCase);
    string[] stringArray;

    private void richTextbox_Loaded(object sender, RoutedEventArgs e)
    {
         var richTextBox= sender as RichTextBox;
         string t = richTextBox.Tag.ToString();
         Paragraph myParagraph = new Paragraph();

         if (rxForbidden.IsMatch(t))
         {
             s = rxForbidden.Split(t);
             richTextBox.Blocks.Add(myParagraph);

             for (int i = 0; i < s.Count(); i++)
             {

                 if (s[i] != null && s[i] != "")
                 {
                     Run txt = new Run();
                     txt.Text = s[i];
                     myParagraph.Inlines.Add(txt);

                 }
                 else
                 {
                     Image MyImage = new Image();
                     MyImage.Source = new BitmapImage(new Uri("/RichTextBoxText;component/smiley_72x72.png", UriKind.Relative));
                     MyImage.Height = 30;
                     MyImage.Width = 30;
                     InlineUIContainer MyUI = new InlineUIContainer();
                     MyUI.Child = MyImage;
                     myParagraph.Inlines.Add(MyUI);
                 }
             }
             richTextBox.Blocks.Add(myParagraph);
         }
private Regex rxForbidden=new Regex(@“[尝试使用此代码

    private Regex rxForbidden = new Regex(@"[<;]", RegexOptions.IgnoreCase);
    string[] stringArray;

    private void richTextbox_Loaded(object sender, RoutedEventArgs e)
    {
         var richTextBox= sender as RichTextBox;
         string t = richTextBox.Tag.ToString();
         Paragraph myParagraph = new Paragraph();

         if (rxForbidden.IsMatch(t))
         {
             s = rxForbidden.Split(t);
             richTextBox.Blocks.Add(myParagraph);

             for (int i = 0; i < s.Count(); i++)
             {

                 if (s[i] != null && s[i] != "")
                 {
                     Run txt = new Run();
                     txt.Text = s[i];
                     myParagraph.Inlines.Add(txt);

                 }
                 else
                 {
                     Image MyImage = new Image();
                     MyImage.Source = new BitmapImage(new Uri("/RichTextBoxText;component/smiley_72x72.png", UriKind.Relative));
                     MyImage.Height = 30;
                     MyImage.Width = 30;
                     InlineUIContainer MyUI = new InlineUIContainer();
                     MyUI.Child = MyImage;
                     myParagraph.Inlines.Add(MyUI);
                 }
             }
             richTextBox.Blocks.Add(myParagraph);
         }

private Regex rxForbidden=new Regex(@)[图像位于何处?这看起来不是有效的URI。它位于应用程序的根上嘿..尝试使用longlistselector而不是listbox来减少渲染问题..并让我知道状态..图像位于何处?这看起来不是有效的URI。它位于应用程序的根上嘿..尝试在中使用longlistselector代替listbox以减少呈现问题..并让我知道状态..不,这不是问题所在。同时我发现:“请注意,XAML属性返回的XAML字符串将不包括内容中存在的任何UIElement对象。InlineUIContainer对象将转换为空运行对象。”这里:意味着我不能通过xaml元素添加图像,所以我现在尝试一种不同的方法。无论如何,谢谢你的回答。啊,这是真的,我现在也记得读过。在我的应用程序中,我决定放一些(半琐碎的)在视图中编写代码以填充RichTextBox,而不是尝试对其进行数据绑定。不,这不是问题。与此同时,我发现:“请注意,XAML属性返回的XAML字符串将不包括内容中存在的任何UIElement对象。InlineUIContainer对象将转换为空运行对象。”这里:意味着我不能通过xaml元素添加图像,所以我现在尝试一种不同的方法。无论如何,谢谢你的回答。啊,这是真的,我现在也记得读过。在我的应用程序中,我决定在视图中放入一些(半琐碎的)代码来填充RichTextBox,而不是尝试数据绑定它。