Silverlight 4 RichTextBox 使用 DataContext 绑定数据

发布于 2024-08-21 04:08:58 字数 535 浏览 4 评论 0原文

我正在使用 Silverlight 4 并尝试将我的测试应用程序设置为多语言,但当我到达“RichTextBox”控件时遇到一些麻烦。我可以通过执行后台代码(c#)来正确绑定它,但是当尝试使用“DataContext”属性时,我根本无法加载它。

我创建了一个 FormatConverter,它返回一个用于测试的块(段落),我的 RichTextBox 代码如下所示:

   <RichTextBox x:Name="rtaTest" BorderThickness="0" IsReadOnly="True" UseLayoutRounding="True" 
DataContext="{Binding Source={StaticResource Localization}, Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}">
    </RichTextBox>

我想知道是否有一种从 XAML 绑定 RichTextBox 的方法。

I am working with Silverlight 4 and trying to put my test apps multilingual but I am having some trouble when I arrive to the "RichTextBox" control. I am able to bind it properly by doing back-code (c#), but when trying using the "DataContext" attributes I am not able to load it at all.

I have created a FormatConverter that return a Block (paragraph) for testing and my code where I have my RichTextBox looks like:

   <RichTextBox x:Name="rtaTest" BorderThickness="0" IsReadOnly="True" UseLayoutRounding="True" 
DataContext="{Binding Source={StaticResource Localization}, Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}">
    </RichTextBox>

I am wondering if there is a way of binding a RichTextBox from the XAML.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

橘味果▽酱 2024-08-28 04:08:58

Run 似乎支持 SL4 中的数据绑定,如下所示:

<RichTextBox>
  <Paragraph>
    <Run Text="{Binding Path=LineFormatted}" />
  </Paragraph>
</RichTextBox>

Run seems to support databinding in SL4, as in:

<RichTextBox>
  <Paragraph>
    <Run Text="{Binding Path=LineFormatted}" />
  </Paragraph>
</RichTextBox>
思慕 2024-08-28 04:08:58

我想您可能对 DataContext 的使用有点困惑。例如,您可能有一些富文本,其中一个或多个 InlineUIContainer 元素的某些子元素可能会从某个对象的属性中检索其文本。您可以将该对象分配给DataContext

虽然我不太确定您期望实现什么,但我怀疑您真正需要的是转换器实际返回一个 BlocksCollection (即使它只包含单个 Block< /code> 你最初返回)然后绑定为:-

<RichTextArea x:Name="rtaTest" BorderThickness="0" IsReadOnly="True"
 UseLayoutRounding="True"
 Blocks="{Binding Source={StaticResource Localization},
   Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}" />

I think you may be a little confused about the used of the DataContext. You might for example have some Rich text where some children of one or more InlineUIContainer elements may retrieve their text from a property of some object. You would assign the object to the DataContext.

Whilst I'm not quite sure what you were expecting to achieve but I suspect that what you really need is for your converter to actually return a BlocksCollection (even if it just contains the single Block you were originaly returning) and then to bind as:-

<RichTextArea x:Name="rtaTest" BorderThickness="0" IsReadOnly="True"
 UseLayoutRounding="True"
 Blocks="{Binding Source={StaticResource Localization},
   Path=Home.MainContent, Converter={StaticResource ParagraphFormatConverter}}" />
〃温暖了心ぐ 2024-08-28 04:08:58

这个 FillFromXml 是 WPF 的东西吗?在 Silverlight 中看不到它。

This FillFromXml is a WPF thing? Don't see it in Silverlight.

留蓝 2024-08-28 04:08:58

块无法设置,只能获取。为 RichTextArea 设置块的一种方法是

public static void UpdateRichTextArea(RichTextArea area, string xmlText)
{
    if (area == null)
    return;

    area.Blocks.FillFromXml(xmlText, true);
}

Blocks cannot be set, they can only be fetched. One way to set the blocks for a RichTextArea is

public static void UpdateRichTextArea(RichTextArea area, string xmlText)
{
    if (area == null)
    return;

    area.Blocks.FillFromXml(xmlText, true);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文