WPF:在单击事件中更改动态创建按钮的背景颜色

时间:2017-01-25 06:11:25

标签: c# wpf xaml button

我在点击事件上创建了一个按钮:

  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;
        }

问题:

我希望按钮点击时只有单击按钮的背景应更改为白色,之前单击的按钮应将其颜色更改回灰色

作为参考,我附上了截图:

On click of first button

On click of second button

更新

我正在为 btnAddNewQuestion 添加 XAML 代码

<Button x:Name="btnAddNewQuestion" Click="btnAddNewQuestion_Click" />

3 个答案:

答案 0 :(得分:4)

spMain.Children中搜索并更改了所有按钮背景,然后点击了下一个更改的当前背景按钮。

 private void btnEnterQuestion_Click(object sender, RoutedEventArgs e)
    {
        foreach (var btn in spMain.Children.OfType<Button>().Where(x=> x.Name.StartsWith("btn")))
            btn.Background =  Brushes.Gray;

        Button button = sender as Button;
        button.Background = Brushes.White;
    }

答案 1 :(得分:0)

这是我使其工作的代码,只是创建了一个新项目并尝试在设计视图上创建了一个按钮,然后以编程方式创建另一个按钮并将其设置在stackpanel中。单击第二个按钮时,在点击事件中,您可以按名称调用第一个按钮并更改其背景...希望这是您所需要的。

代码背后

int counter = 0;
    private void btnAddNewQuestion_Click(object sender, RoutedEventArgs e)
    {
        Button btnQuestion = new Button();
        btnQuestion.Name = "btn" + counter.ToString();
        spMain.Children.Add(btnQuestion);
        btnQuestion.Content = "Enter/Edit your question here";
        btnQuestion.Background = Brushes.Gray;
        btnQuestion.Click += new RoutedEventHandler(btnEnterQuestion_Click);
        counter++;
    }

    private void btnEnterQuestion_Click(object sender, RoutedEventArgs e)
    {
        Button button = sender as Button;
        button.Background = Brushes.White;
        button1.Background = Brushes.Gray; // change of first buttons color
    }

<强> XAML

 <Grid>
    <StackPanel x:Name="spMain">
        <Button x:Name="button1" Content="Button" Click="btnAddNewQuestion_Click"/>
    </StackPanel>
</Grid>

答案 2 :(得分:0)

我想向您展示一个更加注重XAML的方法,其中按钮在视图中作为RadioButton进行管理,在后面的代码中作为QuestionButtonViewModel进行管理。颜色的更改由ControlTemplate.Triggers根据IsChecked属性进行管理。

XAML:

<Grid x:Name="grid1">
    <Grid.Resources>
        <ControlTemplate x:Key="QuestionButtonControlTemplate" TargetType="{x:Type RadioButton}">
            <Border x:Name="buttonBorder" BorderBrush="DarkGray" Background="WhiteSmoke" BorderThickness="1" Padding="5">
                <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Left"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsChecked" Value="True">
                    <Setter TargetName="buttonBorder" Property="BorderBrush" Value="Goldenrod"/>
                    <Setter TargetName="buttonBorder" Property="Background" Value="White"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Grid.Resources>

    <StackPanel Width="300" Margin="5">
        <ItemsControl ItemsSource="{Binding QuestionButtons}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton
                        Content="{Binding QuestionText}"
                        Click="RadioButton_Click"
                        Margin="3"
                        GroupName="QuestionButtons"
                        Template="{StaticResource QuestionButtonControlTemplate}"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

        <Button Content="Add new Question" Click="AddQuestion_Click" Margin="3"/>
    </StackPanel>
</Grid>

代码背后:

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

        QuestionButtons = new ObservableCollection<QuestionButtonViewModel>();

        Loaded += MainWindow_Loaded;
    }

    public ObservableCollection<QuestionButtonViewModel> QuestionButtons { get; set; }

    void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        grid1.DataContext = this;
    }

    private void AddQuestion_Click(object sender, RoutedEventArgs e)
    {
        QuestionButtons.Add(new QuestionButtonViewModel() { QuestionText = "Enter/Edit your question here" });
    }

    private void RadioButton_Click(object sender, RoutedEventArgs e)
    {
        // whatever you want to do, other than setting visual things
    }

}

public abstract class BaseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    protected void RaisePropertyChangedEvent([CallerMemberName]string prop = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(prop));
    }
}

public class QuestionButtonViewModel : BaseViewModel
{
    private string _QuestionText;
    public string QuestionText
    {
        get { return _QuestionText; }
        set
        {
            if (_QuestionText != value)
            {
                _QuestionText = value;
                RaisePropertyChangedEvent();
            }
        }
    }
}