更新整理
This commit is contained in:
44
ShrlAlgoToolkit.Mvvm/Assists/BindingHelper.cs
Normal file
44
ShrlAlgoToolkit.Mvvm/Assists/BindingHelper.cs
Normal file
@@ -0,0 +1,44 @@
|
||||
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;
|
||||
|
||||
}
|
||||
/// <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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user