【问题标题】:C# WPF RichTextBox - HyperlinksC# WPF RichTextBox - 超链接
【发布时间】:2020-07-20 16:51:32
【问题描述】:

我正在寻找一种在 WPF 中的 RichTextBoxes 中自动检测超链接的方法(或者如果有一个适合它的类似控件,我会接受它……只需要一个 TextBox)。

我知道这些解决方案: - Clicking HyperLinks in a RichTextBox without holding down CTRL - WPF - WPF Dynamic HyperLinks RichTextbox

但我的问题是,我使用双向绑定来动态地将 RichTextBoxes 添加到 ItemControl。我的数据模板如下所示:

<RichTextBox IsDocumentEnabled="True" IsReadOnly="True">
   <FlowDocument>
      <Paragraph>
           <Run Text="{Binding MyDataText}"/>
     </Paragraph>
  </FlowDocument>
</RichTextBox>

当我想包含可点击的超链接时,我需要添加它们

<Paragraph>
    <Hyperlink> 
    https://stackoverflow.com
    </Hyperlink>
<Paragraph>

但由于我使用的数据绑定仅包含如上所示的文本 - 而且我不知道另一种解决方案 - 使用已定义的数据模板,我不能使用它。重点是我需要在一个 RichTextBox 中混合普通文本和超链接。

感谢您的帮助,谢谢!

【问题讨论】:

标签: c# wpf textbox richtextbox


【解决方案1】:

您可以使用附加属性来启用与RichTextBox 的数据绑定。然后在检查string值的类型后,实现一个IValueConverter,将string转换为合适的FlowDocument:

RichTextBox.cs

public class RichTextBox : DependencyObject
{
  public static readonly DependencyProperty DocumentSourceProperty = DependencyProperty.RegisterAttached(
    "DocumentSource",
    typeof(FlowDocument),
    typeof(RichTextBox),
    new PropertyMetadata(default(string), RichTextBox.OnDocumentSourceChanged));

  public static void SetText([NotNull] DependencyObject attachingElement, FlowDocument value) =>
    attachingElement.SetValue(RichTextBox.DocumentSourceProperty, value);

  public static string GetText([NotNull] DependencyObject attachingElement) =>
    (FlowDocument) attachingElement.GetValue(RichTextBox.DocumentSourceProperty);


  private static void OnDocumentSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
  {
    if (!(d is System.Windows.Controls.RichTextBox richTextBox))
    {
      throw new ArgumentException($"Wrong type.\nThe attaching element must be of type {typeof(System.Windows.Controls.RichTextBox)}.");
    }

    Application.Current.Dispatcher.InvokeAsync(
      () => richTextBox.Document = (FlowDocument) e.NewValue,
      DispatcherPriority.DataBind);
  }
}

StringToFlowDocumentConverter.cs

[ValueConversion(typeof(string), typeof(FlowDocument))]
public class StringToFlowDocumentConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    if (value is string stringValue)
    {
      return stringValue.StartsWith("http", StringComparison.OrdinalIgnoreCase)
        ? CreateHyperlinkDocument(stringValue)
        : CreateTextDocument(stringValue);
    }

    return Binding.DoNothing;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotSupportedException();
  }

  private FlowDocument CreateHyperlinkDocument(string url)
  {
    return new FlowDocument(new Paragraph(new Hyperlink(new Run(url))));
  }

  private FlowDocument CreateTextDocument(string text)
  {
    return new FlowDocument(new Paragraph(new Run(text)));
  }
}

MainWindow.xaml

<Window>
  <Window.Resources>
    <StringToFlowDocumentConverter x:Key="StringToFlowDocumentConverter" />
  </Window.Resources>

  <RichTextBox RichTextBox.DocumentSource="{Binding MyDataText, Converter={StaticResource StringToFlowDocumentConverter}" />
</Window>

【讨论】:

    猜你喜欢
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-01
    • 2016-05-16
    • 2014-11-15
    • 2013-09-22
    相关资源
    最近更新 更多