简体   繁体   English

使用撤消和重做命令更改RichTextBox工具栏颜色

[英]Change RichTextBox ToolBar Color with Undo and Redo Command

1- Run following code. 1-运行以下代码。

<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">

<StackPanel>
    <ToolBar>
        <Button x:Name="UndoButton" Width="30" CommandTarget="{Binding ElementName=RichTextBox1}" Command="ApplicationCommands.Undo">
            <TextBlock x:Name="UndoTextBlock" Foreground="Gray" FontFamily="Wingdings 3" FontSize="24" Text="Q"/>
        </Button>

        <Button x:Name="RedoButton" Width="30" CommandTarget="{Binding ElementName=RichTextBox1}" Command="ApplicationCommands.Redo">
            <TextBlock x:Name="RedoTextBlock" Foreground="Gray" FontFamily="Wingdings 3" FontSize="24" Text="P"/>
        </Button>
    </ToolBar>

    <RichTextBox x:Name="RichTextBox1">
        <FlowDocument>
            <Paragraph>
                <Run Text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."/>
            </Paragraph>
        </FlowDocument>
    </RichTextBox>
</StackPanel>

</Window> 

2- Check if UndoButton is highlighted when you put your mouse on the UndoButton. 2-将鼠标放在UndoButton上时,检查UndoButton是否突出显示。

3- Delete some text from opening window. 3-从打开的窗口中删除一些文本。

4- Check if UndoButton is highlighted when you put your mouse on the UndoButton. 4-检查将鼠标放在UndoButton上时UndoButton是否突出显示。

5- As you can see if you delete some text then UndoButton is highlighted when you put your mouse on the UndoButton 5-如您所见,如果您删除了一些文本,则将鼠标放在UndoButton上时,UndoButton将突出显示

My question is here; 我的问题在这里;

I want to change Foreground color of UndoTextBlock from Gray to Green when User delete some text (when UndoButton is active). 我想在用户删除一些文本时(当UndoButton处于活动状态时)将UndoTextBlock的前景颜色从灰色更改为绿色

I think can handle the TextChanged event of RichTextBox1. 我认为可以处理RichTextBox1的TextChanged事件。

First, get the original content from RichTextBox. 首先,从RichTextBox获取原始内容。

Then, compare the original content with the new content. 然后,将原始内容与新内容进行比较。

If the content has changed, change the Foreground colour for undoTextBlock to Green. 如果内容已更改,请将undoTextBlock的前景颜色更改为绿色。

    private void RichTextBox1_OnTextChanged(object sender, TextChangedEventArgs e)
    {
        var textRange = new TextRange(RichTextBox1.Document.ContentStart,RichTextBox1.Document.ContentEnd);
        var text = textRange.Text;

        if (string.IsNullOrEmpty(text.Trim()))
            return;

        if (!_loaded)
        {
            _orginalContent = text;
            _loaded = true;
        }

        var newContent = text;
        if (newContent == _orginalContent)
            UndoTextBlock.Foreground = Brushes.Gray;
        else
            UndoTextBlock.Foreground = Brushes.Green;
    }

在此处输入图片说明

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

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