Uwp 当禁用然后启用时,RichEditBox会释放颜色格式

Uwp 当禁用然后启用时,RichEditBox会释放颜色格式,uwp,isenabled,richeditbox,Uwp,Isenabled,Richeditbox,我正在UWP应用程序中使用RichEditBox,我需要在软件中启用和禁用它以执行某些任务。我遇到的问题是,当我禁用然后重新启用它时,任何颜色格式都会丢失。 我在这里创建了一个简单的示例来尝试这个问题。您可以键入一些文本,将其涂成红色,然后禁用并重新启用。颜色恢复为黑色 XAML 这看起来像是RichEditBox组件中的一个bug,我想知道是否有人有一个解决方案,允许我在不丢失颜色格式的情况下启用和禁用文本。 在下面的示例中重现问题。请参见课程中的部分: Color myColor = Col

我正在UWP应用程序中使用RichEditBox,我需要在软件中启用和禁用它以执行某些任务。我遇到的问题是,当我禁用然后重新启用它时,任何颜色格式都会丢失。 我在这里创建了一个简单的示例来尝试这个问题。您可以键入一些文本,将其涂成红色,然后禁用并重新启用。颜色恢复为黑色

XAML

这看起来像是RichEditBox组件中的一个bug,我想知道是否有人有一个解决方案,允许我在不丢失颜色格式的情况下启用和禁用文本。 在下面的示例中重现问题。

请参见课程中的部分:

Color myColor = Colors.Black; // this will act as your toggle of color you change it according to logic of your app in this case I will show you how you can retain the color even when disabled. Whenever you change the color of your richEditBox to red or to any color, just assign that color to it as well. Here I have set it default color to black.

private void OnDisable(object sender, RoutedEventArgs e)
{
    RichEditor.IsEnabled = false;
}

private void OnEnable(object sender, RoutedEventArgs e)
{
    RichEditor.IsEnabled = true;
    //Now after enabling just set the myColor property to RichEditBox Foreground.
    RichEditor.Document.Selection.CharacterFormat.ForegroundColor = myColor;
}

private void OnRed(object sender, RoutedEventArgs e)
{
     RichEditor.Document.Selection.CharacterFormat.ForegroundColor = Colors.Red;
     myColor = Colors.Red; // whatever color you set to the RichEditBox, set it to myColor as well.
}
您可以修改默认样式和ControlTemplate以使控件具有唯一的外观。定义控件外观的默认样式、模板和资源包含在generic.xaml文件中。出于设计目的,可以在Windows软件开发工具包(SDK)安装的(程序文件)\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\generic文件夹中找到generic.xaml。来自不同版本SDK的样式和资源可能具有不同的值

因此,您可以找到System.Windows.Controls.RichEditBox控件的默认样式,以下是1803版的默认样式目标:


从上面的样式中,您可以看到
已禁用
代码:


在该
已禁用
中,您可以发现当您禁用时,
ContentElement
前台
将被
meresource textcontrolforeground禁用
。由于禁用时文本的前景已更改,因此将丢失颜色格式

您可以通过删除更改
ContentElement
前景的
代码来保持颜色格式,如下禁用VisualState:


不幸的是,这不起作用。这项改变不会立即生效。我可以在RichEditor.IsEnabledChanged中进行此更改,但这也不起作用,因为颜色仅应用于文本的一个区域。我可以有一个蓝色单词和一个红色单词的黑色文本。此信息以RichEditBox的RTF格式存储,但在禁用和重新启用控件时颜色会丢失。我唯一的解决办法就是存储
private void OnDisable(object sender, RoutedEventArgs e)
{
    RichEditor.IsEnabled = false;
}

private void OnEnable(object sender, RoutedEventArgs e)
{
    RichEditor.IsEnabled = true;
}

private void OnRed(object sender, RoutedEventArgs e)
{
     RichEditor.Document.Selection.CharacterFormat.ForegroundColor = Colors.Red;
}
Color myColor = Colors.Black; // this will act as your toggle of color you change it according to logic of your app in this case I will show you how you can retain the color even when disabled. Whenever you change the color of your richEditBox to red or to any color, just assign that color to it as well. Here I have set it default color to black.

private void OnDisable(object sender, RoutedEventArgs e)
{
    RichEditor.IsEnabled = false;
}

private void OnEnable(object sender, RoutedEventArgs e)
{
    RichEditor.IsEnabled = true;
    //Now after enabling just set the myColor property to RichEditBox Foreground.
    RichEditor.Document.Selection.CharacterFormat.ForegroundColor = myColor;
}

private void OnRed(object sender, RoutedEventArgs e)
{
     RichEditor.Document.Selection.CharacterFormat.ForegroundColor = Colors.Red;
     myColor = Colors.Red; // whatever color you set to the RichEditBox, set it to myColor as well.
}