Click here to Skip to main content
13,190,405 members (53,395 online)
Click here to Skip to main content

Stats

23.8K views
283 downloads
24 bookmarked
Posted 14 Jan 2010

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

, 14 Jan 2010
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.
Assemblies
Microsoft.Expression.Interactions.dll
System.ComponentModel.Composition.dll
System.CoreEx.dll
System.Interactive.dll
System.Reactive.dll
System.Windows.Interactivity.dll
WexEventTrigger
ClassDiagram1.cd
Wex.csproj.user
bin
Debug
WexEventTrigger.dll
WexEventTrigger.vshost.exe
WexEventTrigger.vshost.exe.manifest
Composition
Lib
Properties
Reactive
WexEventTrigger.Demo
bin
Debug
WexEventTrigger.Demo.vshost.exe
WexEventTrigger.Demo.vshost.exe.manifest
Commands
Extensions
Properties
Views
Demos
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)

Share

About the Author


You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171016.2 | Last Updated 14 Jan 2010
Article Copyright 2010 by Anoop Madhusudanan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid