Click here to Skip to main content
15,885,695 members
Articles / Programming Languages / C#

Adventures While Building a Silverlight Enterprise Application - Part #26

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
26 Oct 2009CPOL1 min read 7.9K  
A more generic solution to databind to some primitive type variables like a bool or a string

The other day, I ran into a scenario where I would need to databind to some primitive type variables like a bool or a string. As I didn't want to build class after class to solve this, I sought after a more generic solution. This article describes that solution.

The Requirements

The requirements are simple:

  • I should be able to databind to any primitive type
  • The solution should be easy to use
  • The solution should require as little code as possible

Analysis

Analyzing this, I quickly realized a generic class, wrapping the actual variable would be a nice solution. I started with this:

C#
public class BindableObject<T>

A simple class declaration taking in any type. Obviously, I needed the class to support binding to its properties, so I included the INotifyPropertyChanged interface in the declaration and I implemented the PropertyChanged event and a method to trigger that event.

I also needed to expose a property to contain the actual value, so I included the Value property and its private field.

Now I would write something like:

C#
BindableObject<bool> someObject = new BindableObject<bool>();
Binding someBinding = new Binding("Value");
someBinding.Source = someObject;
someBinding.Mode = BindingMode.TwoWay;
textBox1.SetBinding(TextBox.IsEnabledProperty, someBinding);

As you can see, this is still quite elaborate. I decided on adding a method to do the binding inside the BindableObject class. I ended up with a class that looks like this:

C#
public class BindableObject<T> : INotifyPropertyChanged
{
private T _value;

public T Value 
{
get
{
return _value;
}
set
{
_value = value;
DoPropertyChanged("Value");
}
}

public void BindTo(FrameworkElement element, 
	DependencyProperty property, BindingMode mode)
{
Binding binding = newBinding("Value");
binding.Source = this;
binding.Mode = mode;
element.SetBinding(property, binding);
}

#region INotifyPropertyChanged Members

private void DoPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, newPropertyChangedEventArgs(propertyName));
}
}

public event PropertyChangedEventHandler PropertyChanged;

#endregion
}

It's a fairly simple class. You can now write the previous example like this:

C#
BindableObject<bool> someObject = new BindableObject<bool>();
someObject.BindTo(textBox1, TextBox.IsEnabledProperty, BindingMode.TwoWay);

I'm pretty happy with this and I hope it proves useful to you too.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) KnowledgePlaza
Netherlands Netherlands
Since early 2001 I've been working full time as a software developer and since 2004 I've been working mostly with Microsoft technology.
I started out as a product developer, but after a few years I switched to a project company where my roles ranged from developer up to consultant and from team lead and coach to manager.
Eventually I switched jobs and focused on the consultant part and then I got back to building a product once again. Now I work in a job where I get to do both.

Comments and Discussions

 
-- There are no messages in this forum --