使用扩展 Toolkit Richtext 框和 mvvm 绑定彩色文本

发布于 2025-01-06 06:55:52 字数 535 浏览 3 评论 0原文

我有一个看起来像这样的类:

public class Line
{
   public Color Color { get; set; }
   public string Text { get; set; }
}

我正在使用 mvvm,现在我有一个将文本绑定到的字符串属性,我的 xaml 看起来像这样

<toolkit:RichTextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Aliased" AcceptsTab="True" AcceptsReturn="True"/>

我最初使用列表框设置了这个并且能够转换我的颜色没有太多麻烦,但是我不知道如何使用 rtb 来做到这一点。我唯一能想到的就是在更新我的属性之前将 rtf 格式添加到字符串中。我猜有一个更简单的解决方案吗?

I have a class that looks something like this:

public class Line
{
   public Color Color { get; set; }
   public string Text { get; set; }
}

I'm using mvvm and right now i'm have have a string properity that i bind Text to, my xaml looks like this

<toolkit:RichTextBox Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}" TextOptions.TextFormattingMode="Ideal" TextOptions.TextRenderingMode="Aliased" AcceptsTab="True" AcceptsReturn="True"/>

I originally had this set up with a list box and was able to Convert my color without to much trouble, however I'm failing to see how to do this with rtb. Only thing i can think of of the top is to add rtf formatting to the string prior to updating my property. I'm guessing there is a simpler solution though?

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

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

发布评论

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

评论(1

温馨耳语 2025-01-13 06:55:52

不确定工具包 RTB 是否支持此功能,但如果您还没有尝试过,您可以看看设置 TextElement.Foreground 是否有效:

<RichTextBox ...>
    <TextElement.Foreground>
        <SolidColorBrush Color="{Binding Color}" />
    </TextElement.Foreground>

Not sure if the toolkit RTB supports this but if you did not try it yet you could see if setting TextElement.Foreground works:

<RichTextBox ...>
    <TextElement.Foreground>
        <SolidColorBrush Color="{Binding Color}" />
    </TextElement.Foreground>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文