I am trying to change to color of button when clicked. It is taking the initial color assigned but not updating when clicked. I attaching my code, let me know where I am going wrong. I tried to implement the code provided in the following post
MainWindow.xaml snippet
<Window.Resources>
<viewModels:MainWindowViewModel x:Key="MainViewModel"/>
</Window.Resources>
<Border Padding="20">
<StackPanel DataContext="{Binding Source={StaticResource MainViewModel}}">
<Button Content="Button1" Margin="10 10 10 10" Command="{Binding ClickCommand, Mode=OneWay}" Background="{Binding BackgroundColorBtn1}"/>
<Button Content="Button2 " Margin="10 10 10 10"></Button>
</StackPanel>
</Border>
</Window>
MainWindow.xaml.cs
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainWindowViewModel();
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
var desktopWorkingArea = System.Windows.SystemParameters.WorkArea;
this.Left = desktopWorkingArea.Right - this.Width;
this.Top = desktopWorkingArea.Bottom - this.Height;
}
}
MainWindowViewModel.cs
namespace DockedPanel.ViewModels
{
public class MainWindowViewModel:INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public MainWindowViewModel()
{
_canExecute = true;
}
private ICommand _clickCommand;
public ICommand ClickCommand
{
get
{
return _clickCommand ?? (_clickCommand = new CommandHandler(() => MyAction(), _canExecute));
}
}
private bool _canExecute;
public void MyAction()
{
_backgroundColorBtn1 = Colors.Blue;
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private Color _backgroundColorBtn1 = Colors.White;
public Color BackgroundColorBtn1
{
get { return _backgroundColorBtn1; }
set
{
if (value == _backgroundColorBtn1)
return;
_backgroundColorBtn1 = value;
OnPropertyChanged(nameof(BackgroundColorBtn1));
}
}
}
}
and finally CommandHandler
namespace DockedPanel.ViewModels.Command
{
public class CommandHandler : ICommand
{
private Action _action;
private bool _canExecute;
public CommandHandler(Action action, bool canExecute)
{
_action = action;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_action();
}
}
}