c# - 使用 WPF 和绑定单击时更改按钮背景颜色
问题描述
我试图在单击时更改按钮的颜色。它采用分配的初始颜色,但在单击时不会更新。我附上了我的代码,让我知道我哪里出错了。我尝试实现以下帖子中提供的代码
MainWindow.xaml 片段
<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>
主窗口.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));
}
}
}
}
最后 是 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();
}
}
}
解决方案
您需要在 BackgroundColorBtn1 属性上调用 OnPropertyChanged,因为您正在更改私有支持变量,并且需要通知 View。
您可以修改 MyAction 方法如下
public void MyAction()
{
_backgroundColorBtn1 = Colors.Blue;
OnPropertyChanged(nameof(BackgroundColorBtn1));
}
或者,您可以直接设置属性而不是支持字段,这将调用 OnPropertyChanged 调用本身。
public void MyAction()
{
BackgroundColorBtn1 = Colors.Blue;
}
您还需要使用颜色到画笔转换器。按钮的背景属性接受画笔,而不是颜色。转换器将允许您将所选颜色转换为画笔。
您可以将转换器定义如下
public class ColorToSolidColorBrushValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return null;
if (value is Color)
return new SolidColorBrush((Color)value);
throw new InvalidOperationException("Unsupported type [" + value.GetType().Name + "], ColorToSolidColorBrushValueConverter.Convert()");
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后,您可以将其用作
Background="{Binding BackgroundColorBtn1, Converter={StaticResource colorToSolidColorBrushConverter}}"
在使用它之前,请确保您已将以下内容添加到您的资源部分
<Window.Resources>
<ResourceDictionary>
<myNameSpace:ColorToSolidColorBrushValueConverter x:Key="colorToSolidColorBrushConverter"/>
</ResourceDictionary>
</Window.Resources>
推荐阅读
- postgresql - 在 macOS 上安装 pg_partman
- node.js - nodejs + socket.io 通过 apache 代理
- c# - 将字符串拆分为两个字符对时出错(Codewars c#)
- angular - 如何在每个 HTTP api 调用中设置不同类型的标头?
- python - 如何计算lightgbm中的重量和价值?
- android - 带有 RequestMultiplePermissions 合同的 ActivityResultLauncher 在启动时不显示权限 UI
- c# - 如何在 .NET Core 中使用带有 MassTransit 的 RabbitMQ 正确使用消息?
- python - 如何使用边界框坐标裁剪图像中的感兴趣区域?
- phpmyadmin - 在 centos 8 中无法使用 yum 安装 phpmyadmin
- javascript - 无法解构对象