Wpf Richtextbox 数据绑定问题:无法获取正确的数据

Wpf Richtextbox data binding issue: can't get the correct data

我的xaml代码如下:

<RichTextBox Grid.Row="4" Width="200" Height="60" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Auto" PreviewKeyDown="PreviewKeyDown">
     <FlowDocument>
          <Paragraph>
              <Run Text="{Binding DisplayText, Mode=TwoWay}" />
          </Paragraph>
     </FlowDocument>
</RichTextBox>

在这个richtextbox中,我先输入了"a",按回车键,然后输入"b",DisplayText的预期值应该是"a b",但实际上它的值是[=20] =].

我的PreviewKeyDown代码如下:

private void PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        if (e.Key == System.Windows.Input.Key.Enter)
        {
            var newPointer = Richtextbox.Selection.Start.InsertLineBreak();
            Richtextbox.Selection.Select(newPointer, newPointer);

            e.Handled = true;
        }
    }

有没有人运行遇到同样的问题?

Sarina,没有直接的方法将字符串变量绑定到 RichTextbox。在 google 上搜索时,您可以找到很多相同的问题陈述。 PreviewKeyDown 仅显示当前键,因此它可能不会显示之前的文本。

简单的方法

设置 RichTextBox文本:

Richtextbox.Document.Blocks.Clear();
Richtextbox.Document.Blocks.Add(new Paragraph(new Run("Text")));

得到 RichTextBox文本:

string richText = new TextRange(Richtextbox.Document.ContentStart, Richtextbox.Document.ContentEnd).Text;

您可以将上述方法与技巧一起使用并获得您想要的输出。您可以在 PreviewKeyDown 事件中手动更新您的模型。

此问题只有一个永久解决方案是使用附件 属性,您可以在其中登录以获取 RichTextBox 的文本。它将帮助您轻松获取和设置您的价值观。

这是 RichTextBox 的文本 属性 的基本代码。您可以根据需要修改它,因为我无法准确地写出您需要的内容。但它一定会帮助您实现您的需求。

public class RichTextBoxHelper
{

    /// <summary>
    /// Text Attached Dependency Property
    /// </summary>
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.RegisterAttached("Text", typeof(string), typeof(RichTextBoxHelper),
            new FrameworkPropertyMetadata((string)null,
                FrameworkPropertyMetadataOptions.BindsTwoWayByDefault |
                FrameworkPropertyMetadataOptions.Journal, 
                new PropertyChangedCallback(OnTextChanged),
                new CoerceValueCallback(CoerceText),
                true,
                UpdateSourceTrigger.PropertyChanged));


    /// <summary>
    /// Gets the Text property.
    /// </summary>
    public static string GetText(DependencyObject d)
    {
        return (string)d.GetValue(TextProperty);
    }

    /// <summary>
    /// Sets the Text property.
    /// </summary>
    public static void SetText(DependencyObject d, string value)
    {
        d.SetValue(TextProperty, value);
    }

    /// <summary>
    /// Returns the Text from a FlowDocument
    /// </summary>
    /// <param name="document">The document to get the text from</param>
    /// <returns>Returns a string with the text of the flow document</returns>
    public static string GetText(FlowDocument document)
    {
        return new TextRange(document.ContentStart, document.ContentEnd).Text;
    }

    /// <summary>
    /// Handles changes to the Text property.
    /// </summary>
    private static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        RichTextBox textBox = (RichTextBox)d;
        if (e.NewValue != null)
        {
            textBox.Document.Blocks.Clear();
            textBox.Document.Blocks.Add(new Paragraph(new Run(e.NewValue.ToString())));
        }
    }
}

XAML绑定

<RichTextBox Grid.Row="4" Width="200" Height="60" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Auto" PreviewKeyDown="PreviewKeyDown" local:RichTextBoxHelper.Text="{Binding DisplayText}">
</RichTextBox>