【发布时间】: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