views:

366

answers:

3

Hi I am in the process of writing my first .net gui. I am wondering if there is some specific way I need to apply to my poco objects for them to be bindable to a usercontrol. I have a few objects but I seem to be unable to bind them to my usercontrol.

I read somewhere that they need to implement IBindable but I can't shake the feeling that someone already has eliminated all that duplicate code I would have to input into all my classes. Is there a way to easily bind these or would I have to use datasets or the like to be easily get this binding working. I have an extreme distaste for datasets to please present some other decent options ;-)

I am trying to bind to usercontrols from the devexpress toolkit.

A: 

With WPF you'll need INotifyPropertyChanged or Dependency Properties. Also look into INotifyCollectionChanged for collections.

David Schmitt
A: 

The BindingList can be used for binding to lists of generic objects.

Aleris
+1  A: 

Which architecture?

For 1-way binding, you don't need anything other than public properties - and maybe some TypeConverter implementations for any bespoke data types (structs etc)

For full 2-way binding, you'll need an eventing implementation - any of:

  • a "public event EventHandler FooChanged" for every property "Foo"
  • an `INotifyPropertyChanged implementation
  • a bespoke component-model (don't go there - overkill)

For an example of a INotifyPropertyChanged implementation (note you might want to move some of the code for re-use) :

public class Foo : INotifyPropertyChanged
{
    private string bar;
    public string Bar
    {
        get { return bar; }
        set { UpdateField(ref bar, value, "Bar"); }
    }
    // other properties...

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null) PropertyChanged(this,
            new PropertyChangedEventArgs(propertyName));
    }
    protected bool UpdateField<T>(ref T field, T value,
        string propertyName)
    {
        if (!EqualityComparer<T>.Default.Equals(field, value))
        {
            field = value;
            OnPropertyChanged(propertyName);
            return true;
        }
        return false;
    }
}

To bind sets of data (grids etc), the easiest thing is to use generics; basically, the minumum is IList - but you get extra metadata from a public T this[int index] indexer - which List<T>, Collection<T> etc all have. More - BindingList<T> implements IBindingList allowing collection-based notification events (but only to INotifyPropertyChanged - not to the FooChanged pattern).

Marc Gravell
I have properties that are of my custom objects as well which do not show up .. Person has property of Address which are both my poco objects. How do I bind Address ?
At the simplest? By overriding ToString() - but how are you showing it? Which control(s)?
Marc Gravell
Textbox, GridView etc.
For textbox, you should be able to use "Address.Line1" etc; for gridview you'll probably need a flattening (facade) object. If you want to edit the *entire* address in the textbox, you'll need a TypeConverter
Marc Gravell