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

WPF Extensibility Hacks or WEX - Includes EventTrigger, ReactiveTrigger, InvokeMethodAction, InvokeCommandAction etc.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
14 Jan 2010CPOL4 min read 37.9K   353   24  
A set of extensibility hacks for WPF. A few interesting triggers and actions, including EventTrigger, ReactiveTrigger, InvokeMethodAction, and InvokeCommandAction. Also allows invoking Triggers and Actions based on Conditions.
using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Xml;
using System.IO;

namespace Wex.Lib.Interactions {


    /// <summary>
    /// I copied this as such from Pete's blog - 
    /// Credits to him
    /// http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html
    /// </summary>
	public static class ConverterHelper2 {

		public static object ConvertToType(object value, Type type) {
			if (value == null)
				return null;

			if (type.IsAssignableFrom(value.GetType()))
				return value;

			TypeConverter converter = TypeDescriptor.GetConverter(value);

			if (converter != null && converter.CanConvertTo(type)  && converter.CanConvertFrom(value.GetType())) {

				value = converter.ConvertFrom(value);
				return value;
			}

            if (type == typeof(bool))
            {
                return bool.Parse(value as string);
            }
            if (type.IsEnum)
            {
                return Enum.Parse(type, value as string, false);
            }

            var result = Convert.ChangeType(value, type);
            if (result != null) return result;

			return null;
		}

	}

     /// <summary>
    /// I copied this as such from Pete's blog - 
    /// Credits to him
    /// http://blois.us/blog/2009/04/datatrigger-bindings-on-non.html
    /// </summary>
	public static class ConverterHelper {

		public static object ConvertToType(object value, Type type) {
			if (value == null)
				return null;

			if (type.IsAssignableFrom(value.GetType()))
				return value;

			TypeConverter converter = ConverterHelper.GetTypeConverter(type);

			if (converter != null && converter.CanConvertFrom(value.GetType())) {

				value = converter.ConvertFrom(value);
				return value;
			}

			return null;
		}

		public static TypeConverter GetTypeConverter(Type type) {
			TypeConverterAttribute attribute = (TypeConverterAttribute)Attribute.GetCustomAttribute(type, typeof(TypeConverterAttribute), false);
			if (attribute != null) {
				try {
					Type converterType = Type.GetType(attribute.ConverterTypeName, false);
					if (converterType != null) {
						return (Activator.CreateInstance(converterType) as TypeConverter);
					}
				}
				catch {
				}
			}
			return new ConvertFromStringConverter(type);
		}
	}

	public class ConvertFromStringConverter : TypeConverter {

		private Type type;

		public ConvertFromStringConverter(Type type) {
			this.type = type;
		}

		public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
			return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
		}

		public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) {
			string stringValue = value as string;
			if (stringValue != null) {
				if (this.type == typeof(bool)) {
					return bool.Parse(stringValue);
				}
				if (this.type.IsEnum) {
					return Enum.Parse(this.type, stringValue, false);
				}
				StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("<ContentControl xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' xmlns:c='" + ("clr-namespace:" + this.type.Namespace + ";assembly=" + this.type.Assembly.FullName.Split(new char[] { ',' })[0]) + "'>\n");
				stringBuilder.Append("<c:" + this.type.Name + ">\n");
				stringBuilder.Append(stringValue);
				stringBuilder.Append("</c:" + this.type.Name + ">\n");
				stringBuilder.Append("</ContentControl>");
                MemoryStream mstream = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(stringBuilder.ToString()));
				ContentControl instance = XamlReader.Load(mstream) as ContentControl;
				if (instance != null) {
					return instance.Content;
				}
			}
			return base.ConvertFrom(context, culture, value);
		}
	}
}


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 Code Project Open License (CPOL)


Written By
Architect
India India
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#

Comments and Discussions