sergeyb replied on Tuesday, July 15, 2008
The way I did it is to create a fake property in CSLA class:
:
Public Property SomeValueIsSet
Get: Return _value
Set: _value = value
Public Property SomeValueIsNotSet
Return Not _value
Set _value = Not Value
Then I bind one radio button to one property, the other button
to the other property.
Does this make sense?
Sergey Barskiy
Senior Consultant
office: 678.405.0687 |
mobile: 404.388.1899
Magenic ®
Microsoft Worldwide Partner of the Year | Custom
Development Solutions, Technical Innovation
From: jfreeman
[mailto:cslanet@lhotka.net]
Sent: Tuesday, July 15, 2008 5:57 PM
To: Sergey Barskiy
Subject: [CSLA .NET] RadioButton Binding
Has anyone had issues with binding to a RadioButton in a WinForm
application? My fields is a bit in the SQL database and I am trying to
setup two RadioButtons based on it. Basically, one is for True and
the other False. Thanks.
Jonathan
tetranz replied on Tuesday, July 15, 2008
The way I've done it is with a simple user control that contains the two radio buttons and has a single boolean "Value" property which binds nicely to a boolean property on my object. It's easily extended to more than two radio buttons to give something other than a boolean.
With radio buttons and checkboxes I change the binding to "OnPropertyChanged" (in advanced settings) so things happen instantly when clicking the control.
I'm sure
Sergey's simpler way works fine too :)
jfreeman: Has anyone had issues with binding to a RadioButton in a WinForm application? My fields is a bit in the SQL database and I am trying to setup two RadioButtons based on it. Basically, one is for True and the other False. Thanks.
tetranz replied on Thursday, July 17, 2008
jfreeman: You wouldn't happen to have some code that you could share, do you? Thanks.
There's not much to it.
Create a user control with your two radio buttons. Let's say they're called radioButton1 and radioButton2. If radioButton2 is checked then we want the value to be true.
Create a property like this: (there's nothing special about the name property name Value).
public bool Value
{
get { return radioButton2.Checked; }
set
{
radioButton1.Checked = !value; // (you might not have to set both but this works for sure)
radioButton2.Checked = value;
}
}
The magic happens by implementing INotifyPropertyChanged. Add INotifyPropertyChanged to the class declaration and this code:
public event PropertyChangedEventHandler PropertyChanged;
private void OnValueChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
Then fire the event when the buttons change:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
OnValueChanged("Value");
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
OnValueChanged("Value");
}
(You could hook them both to the same event.)
Compile and put an instance of the control on your form. It should work like any other control. In DataBindings in the properties list, click Advanced, find the Value field. It will be in "All". There's an attribute which I don't recall that you can add to the property in the control to make it appear in "Common". Bind it to the BO property you want and change the update mode to "OnPropertyChanged".
My code is kind of old so I didn't use INotifyPropertyChanged. I created a plain old EventHandler event called ValueChanged. That works because databinding recognizes events with a name ending in Changed. I think that method has been superseeded by INotifyPropertyChanged. I just did a quick to see that it works.
Cheers
Ross