// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using System.Windows;
using System.Windows.Markup;
namespace ICSharpCode.Core.Presentation
{
///
/// ExtensionMethods that help with WPF.
///
public static class ExtensionMethods
{
///
/// Sets the value of a dependency property on using a markup extension.
///
/// This method does not support markup extensions like x:Static that depend on
/// having a XAML file as context.
public static void SetValueToExtension(this DependencyObject targetObject, DependencyProperty property, MarkupExtension markupExtension)
{
if (targetObject == null)
throw new ArgumentNullException("targetObject");
if (property == null)
throw new ArgumentNullException("property");
if (markupExtension == null)
throw new ArgumentNullException("markupExtension");
var serviceProvider = new SetValueToExtensionServiceProvider(targetObject, property);
targetObject.SetValue(property, markupExtension.ProvideValue(serviceProvider));
}
sealed class SetValueToExtensionServiceProvider : IServiceProvider, IProvideValueTarget
{
readonly DependencyObject targetObject;
readonly DependencyProperty targetProperty;
public SetValueToExtensionServiceProvider(DependencyObject targetObject, DependencyProperty property)
{
this.targetObject = targetObject;
this.targetProperty = property;
}
public object GetService(Type serviceType)
{
if (serviceType == typeof(IProvideValueTarget))
return this;
else
return null;
}
public object TargetObject {
get { return targetObject; }
}
public object TargetProperty {
get { return targetProperty; }
}
}
}
}