- wex.zip
- Assemblies
- Microsoft.Expression.Interactions.dll
- System.ComponentModel.Composition.dll
- System.CoreEx.dll
- System.Interactive.dll
- System.Reactive.dll
- System.Windows.Interactivity.dll
- WexEventTrigger.Demo
- WexEventTrigger.sln
- WexEventTrigger
- bin
- Debug
- ClassDiagram1.cd
- Composition
- Lib
- Properties
- Reactive
- Wex.csproj
- Wex.csproj.user
|
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.
Architect, Developer, Speaker | Wannabe GUT inventor & Data Scientist | Microsoft MVP in C#