|
//
// MarkupScript
// Author: Tom Englert
// Contact: mail@tom-englert.de
// Copyright (C) 2010 tom-englert.de
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Globalization;
namespace MarkupScript
{
/// <summary>
/// Simple type converter to allow setting constant expressions
/// by simply typing the value in XAML.
/// </summary>
public class ExpressionTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
string stringValue = (string)value;
bool booleanResult;
if (bool.TryParse(stringValue, out booleanResult))
{
return new ConstantExpression(booleanResult);
}
double doubleResult;
if (double.TryParse(stringValue, NumberStyles.Any, CultureInfo.InvariantCulture, out doubleResult))
{
return new ConstantExpression(doubleResult);
}
return new ConstantExpression(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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.