tags:

views:

185

answers:

2

Question is for Windows Forms DataBinding

Let's say this is my custom class

public class SimpleClass : INotifyPropertyChanged
{

  public Name
  {get; set;}

  char OrderType;
  ...

}

OrderType is P for purchase order, S for sales order. I for Invoice etc that I need to show as a group of radiobuttons on the Windows Form

for textboxes this is the databinding syntax

Binding nameBinding = new Binding("Text", this.SimpleObject, "Name", true);
this.nameTextBox.DataBindings.Add(nameBinding);

How to databind the OrderType to a groupbox that contains three radiobuttons

A: 

Two issues:

  1. DataBinding works on properties only. As you have it, OrderType is a field.
  2. Binding to RadioButtons in this fashion (as most people would want to do), isn't something natively supported. The only real way to do this is to create a single control that manages one or more RadioButtons and provides a SelectedValue (or equivalent) property, then bind the value to that. There are several third party solutions to this, and I can recommend the DevExpress controls.
Adam Robinson
Oops I forgot to put the property for OrderType. So If I am not using a third party control, than what I take from your explanation is that I need to create a UserControl everytime I need to use radiobuttons inside a groupbox. is that correct?
codemnky
Well, you wouldn't create a UserControl *every time*, but you may need to find an existing custom control or create your own generic version that can support this.
Adam Robinson
A: 

Here's an example of DataBinding a set of RadioButtons (the author calls this a RadioPanel) with a property backed with an enum (in place of your char):

RadioPanel: Binding RadioButton Groups to Enumeration Properties

I haven't tried it.

Otherwise, what worked for me was to create a User Control with a few RadioButtons. The key was to implement INotifyPropertyChanged on the UC and use the RadioButtons' CheckedChanged event to set the value of the property associated with my UC.

Jay Riggs
thanks. i did run into that article while googling. I don't think it should be that involved. If I dont use databinding I could simply check the index on CheckedChanged and update my custom object appropriately. I am just trying to grok my brain around databinding. In some circles(Alt.Net), databinding is not even a recommended practice
codemnky