Skip to content

Instantly share code, notes, and snippets.

@sakapon
Last active July 5, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sakapon/8142421a53e41e1a0f374ee865f65a1e to your computer and use it in GitHub Desktop.
Save sakapon/8142421a53e41e1a0f374ee865f65a1e to your computer and use it in GitHub Desktop.
BindingSample / MarkupExWpf / AppSettingsExtension
using System;
using System.Configuration;
using System.Windows;
using System.Windows.Markup;
namespace MarkupExWpf
{
[MarkupExtensionReturnType(typeof(object))]
public class AppSettingsExtension : MarkupExtension
{
public string Key { get; set; }
public AppSettingsExtension()
{
}
public AppSettingsExtension(string key)
{
Key = key;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (serviceProvider == null) throw new ArgumentNullException(nameof(serviceProvider));
var type = GetTargetPropertyType(serviceProvider);
if (type == null) return DependencyProperty.UnsetValue;
var value = ConfigurationManager.AppSettings[Key];
if (type.IsValueType && value == null) return DependencyProperty.UnsetValue;
return type.IsEnum ?
Enum.Parse(type, value) :
Convert.ChangeType(value, type);
}
static Type GetTargetPropertyType(IServiceProvider serviceProvider)
{
var provider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
if (provider == null) return null;
var property = provider.TargetProperty as DependencyProperty;
if (property == null) return null;
return property.PropertyType;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment