首页 > 解决方案 > 使用 C# 更改按钮背景激活的 MainWindow

问题描述

我正在学习 c# (Visual Studio 2019)。在 MainWindow 中,我有一个带有 page1.xaml 的框架。这个page1 有几个按钮。在激活 MainWindow 时,我想将 page1 中一个按钮的背景更改为红色。我怎么能做到这一点?提前感谢您的任何回答萨宾

标签: c#wpfvisual-studio

解决方案


您必须为按钮提供名称(例如button1),在 WPF 按钮背景中需要为 SolidColorBrush 而不是直接颜色,以便在页面加载时在 InitializeComponent() 之后的 PageFunction 中添加此代码;

        SolidColorBrush brush = new SolidColorBrush(Colors.Red);// convert color to brush            
        button1.Backgroud=brush;// apply the brush

推荐阅读