如何将 rtf 文件加载到 Powershell 中的 WPF RichTextBox [英] How do I load an rtf file to a WPF RichTextBox in Powershell

查看:67
本文介绍了如何将 rtf 文件加载到 Powershell 中的 WPF RichTextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道我可以将 rtf 文件加载到 wpf RichTextBox 吗?

Anyone know hoe I can load an rtf file to a wpf RichTextBox?

在 Windows.Forms 中我会这样做

In Windows.Forms I would do this

RichTextFile.Loadfile(c:\myfile.rtf) 

但我不知道如何在 WPF 中实现相同的功能!

but I don't know how to achieve the same in WPF!

谢谢,

推荐答案

不确定 PowerShell,但 RichTextBox 有一个 Document 属性,您可以使用它来加载 RTF 文件.
这是示例,以及一些对我有帮助的优秀网站:

Not sure about PowerShell, but the RichTextBox has a Document property that you use can load a RTF file.
Here is sample, plus some good sites that helped me:

这是 XAML:

<StackPanel>
    <RichTextBox Height="200" x:Name="rtb"/>
    <Button Content="Load" Click="Button_Click" Width="50" />
</StackPanel>

这是加载 RTF 的按钮点击事件:

Here is the button click event to load the RTF:

public partial class MainView : Window
{
  public MainView()
  {
     InitializeComponent();
  }

  private void Button_Click(object sender, RoutedEventArgs e)
  {
     TextRange textRange;
     System.IO.FileStream fileStream;

     if (System.IO.File.Exists("Document.rtf"))
     {
        textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
        using (fileStream = new System.IO.FileStream("Document.rtf", System.IO.FileMode.OpenOrCreate))
        {
           textRange.Load(fileStream, System.Windows.DataFormats.Rtf);
        }
     }
  }
}

这篇关于如何将 rtf 文件加载到 Powershell 中的 WPF RichTextBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆