【发布时间】:2012-11-19 18:19:36
【问题描述】:
我创建了一个 C# / WPF 应用程序,我想在运行时更改 WPF 表单的颜色,根据条件任务是如果用户选择红色表单更改为红色,如果用户选择绿色表单颜色变为绿色
用户在组合框中选择颜色(红色、绿色),我在组合框单击事件代码中编写代码,如
if ( color.Text== "green")
{
SolidColorBrush bg = new SolidColorBrush(Colors.green);
g.Background = bg; //g is stack panel name
}
else
{
SolidColorBrush bg = new SolidColorBrush(Colors.red);
g.Background = bg;
}
但在运行时我选择了绿色,颜色变为红色而不是绿色。
【问题讨论】:
-
不应将此代码放在 click 事件中,而应放在 SelectedIndexChanged 事件中。