using System.Windows;
using System.Windows.Data;
namespace ShrlAlgoToolkit.Mvvm.Assists
{
public static class BindingHelper
{
public static Binding GetBinding(this FrameworkElement element, DependencyProperty dependencyProperty)
{
var expression = element.GetBindingExpression(dependencyProperty);
if (expression == null)
{
return null;
}
return expression.ParentBinding;
}
///
/// 获取绑定的源(模型视图)属性类型
///
///
///
///
public static Type GetBindingPropertyType(this FrameworkElement element, DependencyProperty dependencyProperty)
{
var binding = element.GetBindingExpression(dependencyProperty);
var source = binding.ResolvedSource;
var prop = source.GetType().GetProperty(binding.ResolvedSourcePropertyName);
return prop.PropertyType;
}
///
/// 设置绑定
///
/// wpf框架元素
/// 依赖属性
/// 视图模型属性
/// 视图模型
public static void SetBinding(this FrameworkElement element,DependencyProperty dependencyProperty,string propertyName,object sourceData)
{
var description = new Binding() { Path = new PropertyPath(propertyName), Source = sourceData };
element.SetBinding(dependencyProperty, description);
}
}
}