我试图在单击时更改按钮的颜色。它采用分配的初始颜色,但单击后不会更新。我附上我的代码,让我知道我要去哪里了。我试图实现以下文章中提供的代码
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>
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));
}
}
}
}
最后 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();
}
}
}
答案 0 :(得分:1)
由于您要更改私有支持变量,因此需要在BackgroundColorBtn1属性上调用OnPropertyChanged,并且需要通知View。
您可以按照以下步骤修改MyAction方法
public void MyAction()
{
_backgroundColorBtn1 = Colors.Blue;
OnPropertyChanged(nameof(BackgroundColorBtn1));
}
或者,您可以直接设置Property而不是backing字段,这会调用OnPropertyChanged调用本身。
public void MyAction()
{
BackgroundColorBtn1 = Colors.Blue;
}
您还需要使用颜色画笔转换器。按钮的background属性接受画笔,而不接受颜色。转换器可让您将所选的颜色转换为画笔。
您可以按以下方式定义Converter
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>