Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用撤消和重做命令更改RichTextBox工具栏颜色_C#_Wpf_Vb.net_Xaml_Command - Fatal编程技术网

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

C# 使用撤消和重做命令更改RichTextBox工具栏颜色,c#,wpf,vb.net,xaml,command,C#,Wpf,Vb.net,Xaml,Command,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>

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-将鼠标放在撤消按钮上时,检查撤消按钮是否高亮显示

3-从打开的窗口中删除一些文本

4-将鼠标放在撤消按钮上时,检查撤消按钮是否高亮显示

5-如您所见,如果删除一些文本,当您将鼠标放在撤消按钮上时,撤消按钮将突出显示

我的问题就在这里


当用户删除某些文本时(当UndoButton处于活动状态时),我想将UndoTextBlock的前景色从灰色更改为绿色。

我认为可以处理RichTextBox1的TextChanged事件

首先,从RichTextBox获取原始内容

然后,将原始内容与新内容进行比较

如果内容已更改,请将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;
    }

我认为可以处理RichTextBox1的TextChanged事件

首先,从RichTextBox获取原始内容

然后,将原始内容与新内容进行比较

如果内容已更改,请将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;
    }

我想更改
,理解,你能发布你尝试过的代码以便我们能提供更好的帮助吗。
我想更改
,理解,你能发布你尝试过的代码以便我们能提供更好的帮助吗。