【发布时间】:2017-01-25 06:11:25
【问题描述】:
我在点击事件上创建了一个按钮:
private void btnAddNewQuestion_Click(object sender, RoutedEventArgs e)
{
Button btnQuestion = new Button();
btnQuestion.Name="btn"+counter.toString();
btnQuestion.Content="Enter/Edit your question here";
btnQuestion.Background=Brushes.Grey;
btnQuestion.Click += new RoutedEventHandler(btnEnterQuestion_Click);
counter++;
}
这里是动态创建按钮的点击事件:
private void btnEnterQuestion_Click(object sender, RoutedEventArgs e)
{
Button button = sender as Button;
button.Background=Brushes.White;
}
问题:
我希望在单击按钮时,仅单击按钮的背景应更改为 White,而之前单击的按钮应将其颜色更改回 Grey
我附上截图供参考:
更新
我正在为 btnAddNewQuestion 添加 XAML 代码
<Button x:Name="btnAddNewQuestion" Click="btnAddNewQuestion_Click" />
【问题讨论】:
-
听起来你想要一个单选按钮组(零个或一个选中)的功能,每个单选按钮都有自定义的视觉表示。欢迎使用 WPF,这是可能的,您不需要太多代码。