Click here to Skip to main content
15,895,746 members
Articles / Desktop Programming / XAML

Silverlight Experimental Hacks (SLEX) - EventTrigger, PropertyTrigger, ReactiveTrigger, InvokeMethodAction, StoryBoardAction, etc. for Silverlight

Rate me:
Please Sign up or sign in to vote.
4.81/5 (8 votes)
14 Jan 2010CPOL6 min read 40.9K   241   24  
A set of Silverlight Experimental Hacks (1) A custom implementation of EventTrigger and PropertyTrigger (2) Invoking methods in your view model in MVVM (3) Conditionally invoking triggers and behaviors (4) ReactiveTrigger for exporting your custom events
using System;
using System.ComponentModel;
using System.Globalization;
using System.Text;
using System.Windows.Controls;
using System.Windows.Markup;

namespace Slex.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 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/client/2007' 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>");
				ContentControl instance = XamlReader.Load(stringBuilder.ToString()) 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