【问题标题】:Richtextbox image with binding not working带有绑定的 Richtextbox 图像不起作用
【发布时间】:2011-11-29 15:29:17
【问题描述】:

转到已编辑

由于 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>";

所以我正在使用文本和字符串解析文本框加载事件上的 Xaml。 XamlTemplate 是带有笑脸模板的硬编码文本。

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

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

【问题讨论】:

  • 您的图片在哪里?这看起来不是一个有效的 URI。
  • 它位于应用程序的根目录
  • 嘿..尝试使用longlistselector而不是listbox来减少渲染问题..让我知道状态..

标签: image xaml windows-phone-7 binding richtextbox


【解决方案1】:

试试这个代码。

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 不,这不是问题。同时我发现:“请注意,Xaml 属性返回的 XAML 字符串将不包括内容中存在的任何 UIElement 对象。InlineUIContainer 对象将转换为空的 Run 对象。”这里:msdn.microsoft.com/en-us/library/ee681613(v=vs.95).aspx 意思是我不能通过 xaml 元素添加图像,所以我现在正在尝试不同的方法。无论如何感谢您的回答。
    • 啊,没错,我记得现在也读过。在我的应用程序中,我决定在视图中放置一些(半平凡的)代码来填充 RichTextBox,而不是尝试对其进行数据绑定。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-16
    • 1970-01-01
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    相关资源
    最近更新 更多