1. 程式人生 > >WPF中RichTextBox的使用小竅門(翻譯 整理)

WPF中RichTextBox的使用小竅門(翻譯 整理)

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

原文在此:http://blogs.msdn.com/jfoscoding/archive/2006/01/14/512825.aspx 

這裡僅整理出其中的知識點:
1. 取得已被選中的內容:
(1)使用 RichTextBox.Document.Selection屬性
(2)訪問RichTextBox.Document.Blocks屬性的“blocks”中的Text

2. 在XAML中增加內容給RichTextBox:
<RichTextBox IsSpellCheckEnabled="True">
   <FlowDocument>
        <Paragraph>
<!-- 這裡加上你的內容 -->
          This is a richTextBox. I can <Bold>Bold</Bold>, <Italic>Italicize</Italic>, <Hyperlink>Hyperlink stuff</Hyperlink> right in my document.
        </Paragraph>
   </FlowDocument>
</RichTextBox>

3. 縮短段間距,類似<BR>,而不是<P>
方法是使用Style定義段間距:
    <RichTextBox>
        <RichTextBox.Resources>
          <Style TargetType="{x:Type Paragraph}">
            <Setter Property="Margin" Value="0"/>


          </Style>
        </RichTextBox.Resources>
        <FlowDocument>
          <Paragraph>
            This is my first paragraph... see how there is...
          </Paragraph>
          <Paragraph>
            a no space anymore between it and the second paragraph?
          </Paragraph>
        </FlowDocument>
      </RichTextBox>

4. 從檔案中讀出純文字檔案後放進RichTextBox或直接將文字放進RichTextBox中: private void LoadTextFile(RichTextBox richTextBox, string filename)
{
    richTextBox.Document.Blocks.Clear();
    using (StreamReader streamReader = File.OpenText(filename)) {
           Paragraph paragraph = new Paragraph();
           paragraph.Text = streamReader.ReadToEnd();
           richTextBox.Document.Blocks.Add(paragraph);
    }
}

private void LoadText(RichTextBox richTextBox, string txtContent)
{
    richTextBox.Document.Blocks.Clear();
    Paragraph paragraph = new Paragraph();
    paragraph.Text = txtContent;
    richTextBox.Document.Blocks.Add(paragraph);
} 5. 取得指定RichTextBox的內容:
private string GetText(RichTextBox richTextBox)
{
        TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
        return textRange.Text;
} 6. 將RTF (rich text format)放到RichTextBox中:         private static void LoadRTF(string rtf, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(rtf)) {
                throw new ArgumentNullException();
            }
            TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
            using (MemoryStream rtfMemoryStream = new MemoryStream()) {
                using (StreamWriter rtfStreamWriter = new StreamWriter(rtfMemoryStream)) {
                    rtfStreamWriter.Write(rtf);
                    rtfStreamWriter.Flush();
                    rtfMemoryStream.Seek(0, SeekOrigin.Begin);                     //Load the MemoryStream into TextRange ranging from start to end of RichTextBox.
                    textRange.Load(rtfMemoryStream, DataFormats.Rtf);
                }
            }
        }
7. 將檔案中的內容載入為RichTextBox的內容
        private static void LoadFile(string filename, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(filename)) {
                throw new ArgumentNullException();
            }
            if (!File.Exists(filename)) {
                throw new FileNotFoundException();
            }
            using (FileStream stream = File.OpenRead(filename)) {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                string ext = System.IO.Path.GetExtension(filename);
                if (String.Compare(ext, ".xaml",true) == 0) {
                    dataFormat = DataFormats.Xaml;
                }
                else if (String.Compare(ext, ".rtf", true) == 0) {
                    dataFormat = DataFormats.Rtf;
                }
                documentTextRange.Load(stream, dataFormat);
            }       
        }
8. 將RichTextBox的內容儲存為檔案:
        private static void SaveFile(string filename, RichTextBox richTextBox)
        {
            if (string.IsNullOrEmpty(filename)) {
                throw new ArgumentNullException();
            }
            using (FileStream stream = File.OpenWrite(filename)) {
                TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
                string dataFormat = DataFormats.Text;
                string ext = System.IO.Path.GetExtension(filename);
                if (String.Compare(ext, ".xaml", true) == 0) {
                    dataFormat = DataFormats.Xaml;
                }
                else if (String.Compare(ext, ".rtf", true) == 0) {
                    dataFormat = DataFormats.Rtf;
                }
                documentTextRange.Save(stream, dataFormat);
            }
        }
9. 做個簡單的編輯器:
  <!-- Window1.xaml -->
  <DockPanel>
    <Menu DockPanel.Dock="Top">
      <MenuItem Header="_File">
        <MenuItem Header="_Open File" Click="OnOpenFile"/>
        <MenuItem Header="_Save" Click="OnSaveFile"/>
        <Separator/>
        <MenuItem Header="E_xit" Click="OnExit"/>
      </MenuItem>     
    </Menu>
    <RichTextBox Name="richTextBox1"></RichTextBox>    
  </DockPanel>
        // Window1.xaml.cs
        private void OnExit(object sender, EventArgs e) {
            this.Close();
        }
        private void OnOpenFile(object sender, EventArgs e) {
            Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
            ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == true) {
                LoadFile(ofd.SafeFileName, richTextBox1);
            }
        }
        private void OnSaveFile(object sender, EventArgs e) {
            Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
            sfd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
            if (sfd.ShowDialog() == true) {
                SaveFile(sfd.SafeFileName, richTextBox1);
            }
        }
           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述