【问题标题】:Selective coloring of the text in TextBox (Windows Store App)TextBox 中文本的选择性着色(Windows 应用商店应用程序)
【发布时间】:2015-02-25 16:01:37
【问题描述】:

我的 MainPage.xaml 中的代码

<TextBox x:Name="TextTextBox" 
          Text="{Binding Path=Text, Mode=TwoWay}"
          TextWrapping="Wrap" />

.
.
.

<!--Button with some Command-->
<Button x:Name="SearchButton" 
                    Content="Search" 
                    HorizontalAlignment="Center"
                    Command="{Binding Command}"/>

我的 ViewModel 中的代码(与 TextBox 绑定的属性)

public string Text
{
    get
    {
        return ssModel.Text;
    }
    set
    {
        ssModel.Text = value;
        OnPropertyChanged();
    }
}

当您按下 SearchButton 时,正在执行返回整数列表的命令(此整数是 TextBox 中着色的索引)。

例如我有文字:

LoremIpsumDolorSitAmet

然后我按下搜索按钮,命令返回给我例如三个数字 {2, 5, 13} 的列表。现在我想在这个位置上为 TextTextBox 字符着色,所以我想得到类似的东西:

这正是我想要得到的。在指定位置为 TextTextBox 中的文本着色。

我将 TextBox 更改为 RichEditBox 并编写 DependencyProperty 以将控件与视图模型中的属性绑定。这是具有 DependencyProperty 的类:

public class RichTextC : DependencyObject
 {
    public static string GetRichText(DependencyObject obj)
    {
        return (string)obj.GetValue(RichTextProperty);
    }

    public static void SetRichText(DependencyObject obj, string value)
    {
        obj.SetValue(RichTextProperty, value);
    }

    public static readonly DependencyProperty RichTextProperty =
        DependencyProperty.Register("RichText", typeof(string), typeof(RichTextC), new PropertyMetadata(string.Empty, callback));

    private static void callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var reb = (RichEditBox)d;
        reb.Document.SetText(TextSetOptions.FormatRtf, (string)e.NewValue);
    }
 }

和 MainPage.xaml 中的 RichEditBox

<RichEditBox local:RichTextC.RichText="{Binding MyRichText, Mode=TwoWay}" 
                     Margin="0,30,0,0""/>

但是我还有一个问题,因为这个 DependencyProperty 只绑定了一种方式。当我在视图模型控件中设置属性的文本/内容时,MainPage 上的 RichEditBox 将被通知并显示新文本。但是当我在 MainPage 更改 RichEditBox 的内容时,它不会通知 View Model 中的属性 - 因此从 View 到 View Model 的绑定不起作用。

【问题讨论】:

    标签: c# textbox windows-store-apps winrt-xaml text-coloring


    【解决方案1】:

    TextBox 不支持多色文本。如果您想要可编辑的彩色文本,则需要使用 RichEditBox。

    但是,没有直接的方法可以绑定到 RichEditBox 的文本。您可以通过 RichEditBox 的ITextDocument 接口以编程方式设置文本及其字符格式。例如,以下将位置 2 设置为红色。您可以在调用 ApplyDisplayUpdates 之前遍历您的整数列表以设置其所有范围:

    ITextDocument doc = rtb.Document;
    ITextRange range = doc.GetRange(2,3);
    range.CharacterFormat.ForegroundColor = Windows.UI.Colors.Red;
    rtb.Document.ApplyDisplayUpdates();
    

    另一种可能性是创建一个包含 RTF 代码的字符串,并使用 ITextDocument.SetText 进行设置。

    如果您想绑定文本,您可以创建一个附加属性,该属性接受 RTF 并调用 SetText,或者接受您自己的更简单的标记脚本并调用 ITextRange.CharacterFormat.ForegroundColor。无论哪种方式,它都类似于我在我的博客条目Binding HTML to a WebView with Attached Properties 中演示的将 HTML 绑定到 WebView

    【讨论】:

    • 感谢您的帖子。我正在尝试使用您的代码,目前我正在测试,所以我在 MainPage 构造函数中使用此代码,但它不起作用。我什么时候应该设置任何文本?我添加这一行 rtb.Document.SetText(Windows.UI.Text.TextSetOptions.None, "TestText"); 然后调用你的代码。
    • 确保范围不为空(请参阅编辑的代码)并从构造函数外部调用它。在您的搜索按钮处理程序中应该没问题。
    • 再次感谢您。我真的很接近解决我的问题。我有另一个问题。你知道如何禁用语言着色/突出显示。我的意思是,我有非英文操作系统,当我用英文在我的 RichEditBox 中书写时,当然几乎所有文本都有红色锯齿线,我想关闭这条线。
    猜你喜欢
    • 2016-06-16
    • 2014-07-05
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2012-10-21
    相关资源
    最近更新 更多