c# - WPF : Remove control's explicit foreground color

标签 c# wpf-controls

标准配色方案中的两个示例 TextBox 和以下构造函数生成具有灰色前景的 Box1 和具有黑色前景的 Box2,因为 Box2 的前景色已被明确设置。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Box2.Foreground = Brushes.Black;
        Box1.IsEnabled = false;
        Box2.IsEnabled = false;
    }
}

我想“取消设置”前景色,以便 Box2 在 IsEnabled 设置为 false 时“回退”到默认禁用颜色并具有灰色前景色。这可能吗?如果是这样,它是如何完成的?

将 Foreground 属性设置为 null 不会产生预期的效果。如果可能,我想避免将前景色显式设置为灰色,因为它与自定义配色方案不兼容。

最佳答案

我不确定这是否是您的意思,但请尝试以下代码:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        Box2.Foreground = Brushes.Black;
        Box1.IsEnabled = false;
        Box2.IsEnabled = false;
        Box2.ClearValue(TextBox.ForegroundProperty);
    }
}

关于c# - WPF : Remove control's explicit foreground color,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5096179/

相关文章:

wpf - 如何在 WPF 中的圆中创建一个带孔的圆?

c# - HttpNotificationChannel 不想创建新的 channel Uri

c# - 无法使用我的身份服务器从 native 应用程序调用 API

c# - 将 blob (.bacpac) 转换为 .bacpac 文件以将数据库导入到 SQL Server Azure?

.net - 在 WPF 中创建透明背景

wpf - 在 WPF 中,如何为模板中设计的 FrameworkElement 添加 EventHandler?

wpf - 在wpf中设置格式化文本的上标和下标

wpf - 如何获取 WPF ItemsControl 的通用 ItemContainer 类型

c# - 无法使用 C# 将带 [] 的索引应用于类型为 'System.Array' 的表达式

c# - 随机程序集引用失败 ("Are you missing a using directive or an assembly reference?")