单选按钮 backColor 的 Winforms INotifyPropertyChanged
Winforms INotifyPropertyChanged for radio button backColor
有没有办法改变已经绑定到 ViewModel
属性 的 RadioButton
的 BackColor
?
基本上我使用的是 ViewModel
和 INotifyPropertyChanged
,我想根据绑定到的 Bool 属性 更改单选按钮的 BackColor
。因此,如果 true
则将背景色设置为 Green
否则 default
.
到目前为止,这是我的尝试,但显然行不通。我很确定我的双重绑定不正确?
public class ViewModel : INotifyPropertyChanged
{
private bool _prop1;
public bool Prop1 {
get
{
return _prop1;
}
set
{
if (value == _prop1) return;
_prop1= value;
OnPropertyChanged();
OnPropertyChanged(nameof(BackColor));
}
}
private bool _prop2;
public bool Prop2{
get
{
return _prop2;
}
set
{
if (value == _prop2) return;
_prop2= value;
OnPropertyChanged();
OnPropertyChanged(nameof(BackColor));
}
}
public Color BackColor => Color.Green;
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
表格
public partial class Form1
{
private ViewModel _viewModel;
public Form1()
{
InitializeComponent();
SetupBindings();
}
public void SetupBindings()
{
radioButton1.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop1), true);
radioButton1.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.BackColor ), true, DataSourceUpdateMode.OnPropertyChanged);
radioButton2.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop2), true);
radioButton2.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.BackColor ), true, DataSourceUpdateMode.OnPropertyChanged);
}
}
好的,我想我的问题已经解决了:)
因此为我的 ViewModel
中的每个单选按钮 BackColor
创建了两个单独的属性
private bool _prop1;
public bool Prop1 {
get
{
return _prop1;
}
set
{
if (value == _prop1) return;
_prop1= value;
OnPropertyChanged();
OnPropertyChanged(nameof(Radio1ButtonBackColor));
}
}
private bool _prop2;
public bool Prop2{
get
{
return _prop2;
}
set
{
if (value == _prop2) return;
_prop2= value;
OnPropertyChanged();
OnPropertyChanged(nameof(Radio2ButtonBackColor));
}
}
public Color Radio1ButtonBackColor => Prop1? Color.Green : default;
public Color Radio2ButtonBackColor => Prop2? Color.Green : default;
并将它们绑定到单选按钮 backColor
属性
public void SetupBindings()
{
radioButton1.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop1), true);
radioButton1.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.RadioButton1BackColor Radio1ButtonBackColor), true, DataSourceUpdateMode.OnPropertyChanged);
radioButton2.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop2), true);
radioButton2.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.RadioButton2BackColor), true, DataSourceUpdateMode.OnPropertyChanged);
}
有没有办法改变已经绑定到 ViewModel
属性 的 RadioButton
的 BackColor
?
基本上我使用的是 ViewModel
和 INotifyPropertyChanged
,我想根据绑定到的 Bool 属性 更改单选按钮的 BackColor
。因此,如果 true
则将背景色设置为 Green
否则 default
.
到目前为止,这是我的尝试,但显然行不通。我很确定我的双重绑定不正确?
public class ViewModel : INotifyPropertyChanged
{
private bool _prop1;
public bool Prop1 {
get
{
return _prop1;
}
set
{
if (value == _prop1) return;
_prop1= value;
OnPropertyChanged();
OnPropertyChanged(nameof(BackColor));
}
}
private bool _prop2;
public bool Prop2{
get
{
return _prop2;
}
set
{
if (value == _prop2) return;
_prop2= value;
OnPropertyChanged();
OnPropertyChanged(nameof(BackColor));
}
}
public Color BackColor => Color.Green;
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
表格
public partial class Form1
{
private ViewModel _viewModel;
public Form1()
{
InitializeComponent();
SetupBindings();
}
public void SetupBindings()
{
radioButton1.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop1), true);
radioButton1.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.BackColor ), true, DataSourceUpdateMode.OnPropertyChanged);
radioButton2.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop2), true);
radioButton2.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.BackColor ), true, DataSourceUpdateMode.OnPropertyChanged);
}
}
好的,我想我的问题已经解决了:)
因此为我的 ViewModel
中的每个单选按钮BackColor
创建了两个单独的属性
private bool _prop1;
public bool Prop1 {
get
{
return _prop1;
}
set
{
if (value == _prop1) return;
_prop1= value;
OnPropertyChanged();
OnPropertyChanged(nameof(Radio1ButtonBackColor));
}
}
private bool _prop2;
public bool Prop2{
get
{
return _prop2;
}
set
{
if (value == _prop2) return;
_prop2= value;
OnPropertyChanged();
OnPropertyChanged(nameof(Radio2ButtonBackColor));
}
}
public Color Radio1ButtonBackColor => Prop1? Color.Green : default;
public Color Radio2ButtonBackColor => Prop2? Color.Green : default;
并将它们绑定到单选按钮 backColor
属性
public void SetupBindings()
{
radioButton1.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop1), true);
radioButton1.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.RadioButton1BackColor Radio1ButtonBackColor), true, DataSourceUpdateMode.OnPropertyChanged);
radioButton2.DataBindings.Add("Checked", _viewModel, nameof(_viewModel.Prop2), true);
radioButton2.DataBindings.Add("BackColor", _viewModel, nameof(_viewModel.RadioButton2BackColor), true, DataSourceUpdateMode.OnPropertyChanged);
}