简体   繁体   English

如何在Powershell中将rtf文件加载到WPF RichTextBox

[英]How do I load an rtf file to a WPF RichTextBox in Powershell

Anyone know hoe I can load an rtf file to a wpf RichTextBox? 有人知道我可以将rtf文件加载到wpf RichTextBox吗?

In Windows.Forms I would do this 在Windows.Forms中,我会这样做

RichTextFile.Loadfile(c:\myfile.rtf) 

but I don't know how to achieve the same in WPF! 但是我不知道如何在WPF中实现相同的目标!

Thanks, 谢谢,

Ben

Not sure about PowerShell, but the RichTextBox has a Document property that you use can load a RTF file. 不确定PowerShell,但是RichTextBox具有一个Document属性,您可以使用该属性来加载RTF文件。
Here is sample, plus some good sites that helped me: 这是示例,以及一些对我有所帮助的优秀网站:

Here is the XAML: 这是XAML:

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

Here is the button click event to load the RTF: 这是加载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);
        }
     }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM