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

一世执手 提交于 2020-01-24 15:57:25

问题


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

In Windows.Forms I would do this

RichTextFile.Loadfile(c:\myfile.rtf) 

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

Thanks,

Ben


回答1:


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:

  • Some tricks for working with WPF’s RichTextBox
  • WPF RichTextBox
  • WPF RichTextBox: How To Load, Edit and Save Rich Text Format

Here is the 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:

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);
        }
     }
  }
}


来源:https://stackoverflow.com/questions/3503285/how-do-i-load-an-rtf-file-to-a-wpf-richtextbox-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!