48 lines
1.8 KiB
C#
48 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using System.Windows;
|
|
using System.Windows.Data;
|
|
|
|
namespace Sai.Toolkit.Mvvm
|
|
{
|
|
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;
|
|
|
|
}
|
|
/// <summary>
|
|
/// 获取绑定的源(模型视图)属性类型
|
|
/// </summary>
|
|
/// <param name="element"></param>
|
|
/// <param name="dependencyProperty"></param>
|
|
/// <returns></returns>
|
|
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;
|
|
}
|
|
/// <summary>
|
|
/// 设置绑定
|
|
/// </summary>
|
|
/// <param name="element">wpf框架元素</param>
|
|
/// <param name="dependencyProperty">依赖属性</param>
|
|
/// <param name="propertyName">视图模型属性</param>
|
|
/// <param name="sourceData">视图模型</param>
|
|
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);
|
|
}
|
|
}
|
|
}
|