2014-12-02 87 views
1

我正在WPF中创建一个文本编辑器程序,我需要制作一个Plain按钮,它的目的是删除格式(粗体,在RichTextBox的文本的斜体,如Arial字体,字体大小)如何从richtextbox中删除任何格式(粗体,斜体,字体,字体大小)WPF

这是我到目前为止的代码:

<Window x:Class="TextEditor.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:TextEditor" 
    Title="Text Editor" Height="480" Width="640"> 
<DockPanel> 
    <Menu DockPanel.Dock="Top"> 

     <MenuItem Header="_File" Name="Menu"> 
      <MenuItem Header="New" Name="New" 
         Click="New_Click" 

       /> 

      <MenuItem Header="Save" Name="Save" 
         Click="Save_Click" 
        /> 

<DockPanel DockPanel.Dock="Top"> 


      <ToolBar> 

      <ToggleButton x:Name="boldButton" 
          ToolTip="Bold" 
          Command="{x:Static EditingCommands.ToggleBold}" CommandTarget="{Binding ElementName=_richTextBox}"> 

       <Image Source="Icons/text_bold.png" 
         Height="25" 
         Width="25"/> 

       </ToggleButton> 

       <ToggleButton x:Name="italicButton" 
          ToolTip="Italic" 
          Command="{x:Static EditingCommands.ToggleItalic}" CommandTarget="{Binding ElementName=_richTextBox}"> 
        <Image Source="Icons/text_italic.png" 
         Height="25" 
         Width="25"/> 


       </ToggleButton> 

      <ToggleButton x:Name="PlainButton" 
        ToolTip="Make the text plain" 
        Click="PlainButton_Click"> 
       PlainText 
      </ToggleButton> 

      <Button x:Name="BackgroundButton" 
        ToolTip="Change the background of the textbox" 
        Click="BackgroundButton_Click"> 
        Change the background 


      </Button> 








       <Separator/> 

       <ComboBox x:Name="fonts" 
         MinWidth="100" 
         DataContext="{x:Static Fonts.SystemFontFamilies}" 
         ItemsSource="{Binding}" 
         ToolTip="Font" 
         SelectionChanged="Font_SelectionChanged"/> 

      <ComboBox x:Name="fontSize" 
         MinWidth="40" 
         ToolTip="Font Size" 

         > 






      </ComboBox> 

      </ToolBar> 


     </DockPanel> 











     <StatusBar DockPanel.Dock="Bottom"> 
     <TextBlock x:Name="status"/>    
    </StatusBar> 

    <RichTextBox x:Name="body" 
       FontSize="{Binding ElementName=fontSize, Path=SelectedItem}" 

       SpellCheck.IsEnabled="True" 
       AcceptsReturn="True" 
       AcceptsTab="True" 
       SelectionChanged="body_SelectionChanged" 
       BorderThickness="0 2 0 0"/> 






</DockPanel> 

升C脚本:

public MainWindow() 
    { 
     InitializeComponent(); 
     for (double i = 8; i <= 48; i += 2) 
     { 
      fontSize.Items.Add(i); 
     } 




    } 



    private void body_SelectionChanged(object sender, RoutedEventArgs e) 
    { 

     object temp = body.Selection.GetPropertyValue(Inline.FontFamilyProperty); 
     fonts.SelectedItem = temp; 



    } 



    private void New_Click(object sender, RoutedEventArgs e) 
    { 
     body.Document.Blocks.Clear(); 
    } 

    private void Save_Click(object sender, RoutedEventArgs e) 
    { 
     SaveFileDialog sfd = new SaveFileDialog(); 
     sfd.Filter = "Text file|*.txt"; 
     sfd.FileName = "Untitled document"; 

     if (sfd.ShowDialog() == true) 
     { 
      FileStream fileStream = new FileStream(sfd.FileName, FileMode.Create); 
      TextRange range = new TextRange(body.Document.ContentStart, body.Document.ContentEnd); 
      range.Save(fileStream, DataFormats.Text); 
     } 
    } 

    private void Font_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     if (fonts.SelectedItem != null) 
      body.Selection.ApplyPropertyValue(Inline.FontFamilyProperty, fonts.SelectedItem); 
    } 



    private void PlainButton_Click(object sender, RoutedEventArgs e) 
    { 

     //the code of the plain button 

    } 

    private void BackgroundButton_Click(object sender, RoutedEventArgs e) 
    { 
     body.Background = Brushes.Yellow; 

    } 

回答

0

如果您仍在寻找答案,请使用以下WPF代码。

myRichTextBox.SelectAll(); 
myRichTextBox.Selection.ClearAllProperties(); 
相关问题