简体   繁体   English

WPF 按钮背景在单击时更改并在单击同一 StackPanel 中的另一个按钮时重置为原始状态。

[英]WPF Button Background Change on Click and Reset to original when another button in same StackPanel Clicked.

I have a StackPanel with a few buttons.我有一个带有几个按钮的 StackPanel。 I want to change the color of a button when clicked and reset it to the original when another button in StackPanel is clicked.我想在单击时更改按钮的颜色,并在单击 StackPanel 中的另一个按钮时将其重置为原始颜色。 Is it possible with a single style applied on StackPanel or I have to create Style for each button?是否可以在 StackPanel 上应用单一样式,或者我必须为每个按钮创建样式? If yes then how.如果是,那么如何。

Here is the code of Style applied to StackPanel but this changes the color of the button but does not reset it on clicking another button.这是应用于 StackPanel 的样式代码,但这会更改按钮的颜色,但不会在单击另一个按钮时重置它。

<Style TargetType="StackPanel" x:Key="GlobalStackPanelStyle" BasedOn="{StaticResource FlatStackPanel}">
      <Style.Resources>
            <Style TargetType="Button">
                <Setter Property="Button.Background" Value="Blue"/>
                <Style.Triggers> 
                    <Trigger Property="IsPressed" Value="True">
                        <Trigger.EnterActions>
                            <BeginStoryboard>
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetProperty="(Button.Background).(SolidColorBrush.Color)" To="Green"/>
                                </Storyboard>
                            </BeginStoryboard>
                        </Trigger.EnterActions>                           
                    </Trigger>                        
                </Style.Triggers>
            </Style>
        </Style.Resources>
    </Style>

Sorry for the delayed response.回复较晚,抱歉。 Here are the steps that you can follow to get the required output.您可以按照以下步骤获取所需的输出。

Assuming you are following MVVM design pattern.假设您遵循 MVVM 设计模式。

  1. Create buttons in .xaml and bind command to each button as shown below,在 .xaml 中创建按钮并将命令绑​​定到每个按钮,如下所示,

     <Button Height="32" Width="180" Grid.Column="1" Content="Button 1" Command="{Binding ClickCommand}" CommandParameter="Button 1"> <Button.Style> <Style TargetType="{x:Type Button}"> <Style.Triggers> <DataTrigger Binding="{Binding IsButton1Active}" Value="True"> <Setter Property="Background" Value="Green" /> <Setter Property="Foreground" Value="White" /> </DataTrigger> <DataTrigger Binding="{Binding IsButton1Active}" Value="False"> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button> <Button Height="32" Width="180" Grid.Column="2" Content="Button 2" Margin="5,0,0,0" Command="{Binding ClickCommand}" CommandParameter="Button 2"> <Button.Style> <Style TargetType="{x:Type Button}"> <Style.Triggers> <DataTrigger Binding="{Binding IsButton2Active}" Value="True"> <Setter Property="Background" Value="Green" /> <Setter Property="Foreground" Value="White" /> </DataTrigger> <DataTrigger Binding="{Binding IsButton2Active}" Value="False"> <Setter Property="Background" Value="Red" /> </DataTrigger> </Style.Triggers> </Style> </Button.Style> </Button>

Note: You can add how many buttons you want to add to above xaml.注意:您可以在 xaml 上方添加要添加的按钮数量。

  1. Create 2 boolean properties and set the values of those boolean properties from your ClickCommand method.创建 2 个布尔属性并从 ClickCommand 方法设置这些布尔属性的值。

     private bool isButton1Active; private bool isButton2Active; public bool IsButton1Active { get { return isButton1Active; } set { isButton1Active = value; OnPropertyChanged(); } } public bool IsButton2Active { get { return isButton2Active; } set { isButton2Active = value; OnPropertyChanged(); } }
  2. Here is code for command - Add it in your ViewModel这是命令的代码 - 将其添加到您的 ViewModel 中

     private UICommand _clickCommand; public UICommand ClickCommand { get { return _clickCommand; } }
  3. Write below statement in your view model constructor在您的视图模型构造函数中写下以下语句

     public YourViewModelConstructor() { _clickCommand = new UICommand(OnClick); }
  4. Here is the method that is bound to ClickCommand这是绑定到 ClickCommand 的方法

     private void OnClick(object parameter) { switch(parameter.ToString()) { case "Button 1": IsButton1Active = true; IsButton2Active = false; break; case "Button 2": IsButton2Active = true; IsButton1Active = false; break; } }
  5. Here is the code for my UICommand class这是我的 UICommand 类的代码

     public class UICommand : ICommand { private readonly Action<object> _execute; private readonly Func<bool> _canExecute; public UICommand(Action<object> onExecuteMethod, Func<bool> onCanExecuteMethod = null) { _execute = onExecuteMethod; _canExecute = onCanExecuteMethod; } public bool IsCanExecute { get; set; } #region ICommand Members public event EventHandler CanExecuteChanged { add { if (_canExecute != null) CommandManager.RequerySuggested += value; } remove { if (_canExecute != null) CommandManager.RequerySuggested -= value; } } public void Execute(object parameter) { _execute(parameter); } public bool CanExecute(object parameter) { IsCanExecute = (_canExecute == null || _canExecute()); return IsCanExecute; } #endregion public void RaiseCanExecuteChanged() { CommandManager.InvalidateRequerySuggested(); } }

I assume you know on how to setting datacontext to your window.我假设您知道如何为您的窗口设置数据上下文。

This examples gives you an idea on how to achieve functionality by creating some properties created in your ViewModel and bind a command in your View to ViewModel Command property and invoke click command by passing Command Parameter.此示例让您了解如何通过创建在 ViewModel 中创建的一些属性来实现功能,并将视图中的命令绑定到 ViewModel 命令属性,并通过传递命令参数调用单击命令。

Still have any doubts after implementing the solution, kindly let us know.实施解决方案后仍有任何疑问,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM