更新整理
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
25
ShrlAlgoToolkit.Mvvm/Assists/BindingProxy.cs
Normal file
25
ShrlAlgoToolkit.Mvvm/Assists/BindingProxy.cs
Normal file
@@ -0,0 +1,25 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace ShrlAlgoToolkit.Mvvm.Assists
|
||||
{
|
||||
/// <summary>
|
||||
/// 绑定代理,用来传递绑定对象
|
||||
/// </summary>
|
||||
public class BindingProxy : Freezable
|
||||
{
|
||||
protected override Freezable CreateInstanceCore() => new BindingProxy();
|
||||
|
||||
public object Data
|
||||
{
|
||||
get { return GetValue(DataProperty); }
|
||||
set { SetValue(DataProperty, value); }
|
||||
}
|
||||
|
||||
// Using a DependencyProperty as the backing store for Data. This enables animation, styling, binding, etc...
|
||||
public static readonly DependencyProperty DataProperty = DependencyProperty.Register(
|
||||
nameof(Data),
|
||||
typeof(object),
|
||||
typeof(BindingProxy)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user