Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / WPF

BindingHub - A new component and Design Pattern, very useful in WPF as well as in ViewModels

Rate me:
Please Sign up or sign in to vote.
4.78/5 (28 votes)
14 Jan 2011Eclipse7 min read 82.4K   501   75  
BindingHub is the best thing since sliced bread. After you finish reading this article, you will start wondering how you could survive for so long without BindingHub. I did wonder the same thing after creating it.
// Copyright (C) Michael Agroskin 2010
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;

namespace WPF.MichaelAgroskin.Converters
{
    public class BrushValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Convert(value, targetType);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return Convert(value, targetType);
        }

        private static readonly BrushConverter _converter = new BrushConverter();

        private static object Convert(object value, Type targetType)
        {
            if (value == null)
                return Binding.DoNothing;

            var sourceType = value.GetType();

            if (typeof(Brush).IsAssignableFrom(sourceType) && _converter.CanConvertTo(null, targetType))
            {
                return _converter.ConvertTo(null, CultureInfo.CurrentUICulture, value, targetType);
            }

            if (typeof(Brush).IsAssignableFrom(targetType))
            {
                if (_converter.CanConvertFrom(null, sourceType))
                {
                    return _converter.ConvertFrom(null, CultureInfo.CurrentUICulture, value);
                }
                if (sourceType == typeof(Color))
                {
                    return new SolidColorBrush((Color)value);
                }
            }

            return Binding.DoNothing;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Eclipse Public License 1.0


Written By
Software Developer (Senior) Liquidnet
United States United States
Michael is a software developer who still remembers punch cards, computers with 4 Kbytes RAM, and 3270s. His personal computers were Apple IIe, Commodore, and PC XT (with the whole 640 Kbytes RAM and 2 floppy drives!!!). Wow, that was a powerhouse.

Fast forward 32 years through FORTRAN, PL-I, Algol, Pascal, Prolog, LISP, C, Basic, Clipper, Assembly, FoxPro, DHTML, JavaScript, C++, you name it, to C# 4.0.

Of course, real men use machine code to write software, but what a difference a few years make! No more mallocs and callocs, GC magically collects unused objects, dynamic objects magically call IUnknown::QueryInterface, Reflection magically gives you metadata and even generates help files, WPF magically binds stuff together...

Read some of Michael's articles here.

BindingHub (a WPF component and a design pattern) [^].

Notifying parent of changes to children's properties [^].

Point-In-Time database (coming soon)

Composite Menus and other ItemsControls (coming soon)

Adorners framework (coming soon)

Drag-n-drop data transfer framework (coming soon)

Converters and MarkupExtensions (coming soon)

Download complete WPF library [^].

Comments and Discussions