【问题标题】:How to remove any formatting(bold, italic, fonts, font size) from a richtextbox WPF如何从 Richtextbox WPF 中删除任何格式(粗体、斜体、字体、字体大小)
【发布时间】:2015-01-30 21:31:49
【问题描述】:

我正在 WPF 中制作一个文本编辑器程序,我需要制作一个普通按钮,其目的是删除 RichTextBox 文本的格式(粗体、斜体、Arial 等字体、字体大小)。

这是我目前的代码:

<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" />
             </MenuItem>
        </Menu>

        <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>
</Window>  

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

【问题讨论】:

    标签: wpf format richtextbox


    【解决方案1】:

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

    myRichTextBox.SelectAll();
    myRichTextBox.Selection.ClearAllProperties();
    

    【讨论】:

      【解决方案2】:

      这是 2009 年的解决方案……我不知道 .NET 中的富文本 API 是否有所改进,但 this post seems to have a solution for what you need

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-09
        • 2011-07-18
        • 2014-03-01
        • 1970-01-01
        • 2016-01-02
        相关资源
        最近更新 更多