优化绑定写法
This commit is contained in:
parent
4254f39fc5
commit
7b64b82032
@ -212,4 +212,104 @@ namespace CPF
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
public class AttachedDescribe : BindingDescribe
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
public AttachedDescribe(object value, string sourceProperty) : base(sourceProperty)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
/// <param name="binding"></param>
|
||||||
|
public AttachedDescribe(object value, string sourceProperty, BindingMode binding) : base(sourceProperty, binding)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
/// <param name="binding"></param>
|
||||||
|
public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding) : base(source, sourceProperty, binding)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
public AttachedDescribe(object value, object source, string sourceProperty) : base(source, sourceProperty)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
/// <param name="convert"></param>
|
||||||
|
public AttachedDescribe(object value, object source, string sourceProperty, Func<object, object> convert) : base(source, sourceProperty, convert)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
/// <param name="binding"></param>
|
||||||
|
/// <param name="convert"></param>
|
||||||
|
public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding, Func<object, object> convert) : base(source, sourceProperty, binding, convert)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
/// <param name="binding"></param>
|
||||||
|
/// <param name="convert"></param>
|
||||||
|
/// <param name="convertBack"></param>
|
||||||
|
public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding, Func<object, object> convert, Func<object, object> convertBack) : base(source, sourceProperty, binding, convert, convertBack)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 设置和绑定附加属性
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="value"></param>
|
||||||
|
/// <param name="source"></param>
|
||||||
|
/// <param name="sourceProperty"></param>
|
||||||
|
/// <param name="binding"></param>
|
||||||
|
/// <param name="convert"></param>
|
||||||
|
/// <param name="convertBack"></param>
|
||||||
|
/// <param name="SourceToTargetError"></param>
|
||||||
|
/// <param name="TargetToSourceError"></param>
|
||||||
|
public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding, Func<object, object> convert, Func<object, object> convertBack, Action<Binding, object, Exception> SourceToTargetError, Action<Binding, object, Exception> TargetToSourceError) : base(source, sourceProperty, binding, convert, convertBack, SourceToTargetError, TargetToSourceError)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public object Value { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -64,7 +64,7 @@ namespace CPF
|
|||||||
this.TargetToSourceError = TargetToSourceError;
|
this.TargetToSourceError = TargetToSourceError;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BindingDescribe(Action<CpfObject, object> command)
|
public BindingDescribe(CommandDescribe command)
|
||||||
{
|
{
|
||||||
Command = command;
|
Command = command;
|
||||||
}
|
}
|
||||||
@ -78,7 +78,7 @@ namespace CPF
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 简化绑定命令的命令,如果设置了该属性,则使用命令绑定
|
/// 简化绑定命令的命令,如果设置了该属性,则使用命令绑定
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Action<CpfObject, object> Command { get; set; }
|
public CommandDescribe Command { get; set; }
|
||||||
|
|
||||||
//public CpfObject Owner { get; internal set; }
|
//public CpfObject Owner { get; internal set; }
|
||||||
|
|
||||||
@ -349,7 +349,7 @@ namespace CPF
|
|||||||
{
|
{
|
||||||
return new BindingDescribe { PropertyName = sourceProperty };
|
return new BindingDescribe { PropertyName = sourceProperty };
|
||||||
}
|
}
|
||||||
public static explicit operator BindingDescribe(Action<CpfObject, object> command)
|
public static implicit operator BindingDescribe(CommandDescribe command)
|
||||||
{
|
{
|
||||||
return new BindingDescribe { Command = command };
|
return new BindingDescribe { Command = command };
|
||||||
}
|
}
|
||||||
@ -392,5 +392,50 @@ namespace CPF
|
|||||||
return base.GetHashCode();
|
return base.GetHashCode();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 命令绑定
|
||||||
|
/// </summary>
|
||||||
|
public class CommandDescribe
|
||||||
|
{
|
||||||
|
public Action<CpfObject, object> Action { get; set; }
|
||||||
|
|
||||||
|
public string MethodName { get; set; }
|
||||||
|
public object[] Parameters { get; set; }
|
||||||
|
|
||||||
|
public object Target { get; set; }
|
||||||
|
|
||||||
|
public Func<UIElement, UIElement> Find { get; set; }
|
||||||
|
/// <summary>
|
||||||
|
/// 用委托定义个命令绑定
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="command"></param>
|
||||||
|
public CommandDescribe(Action<CpfObject, object> command)
|
||||||
|
{
|
||||||
|
Action = command;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 定义个命令绑定
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="methodName"></param>
|
||||||
|
/// <param name="obj"></param>
|
||||||
|
/// <param name="ps"></param>
|
||||||
|
public CommandDescribe(string methodName, object obj = null, params object[] ps)
|
||||||
|
{
|
||||||
|
MethodName = methodName;
|
||||||
|
Parameters = ps;
|
||||||
|
Target = obj;
|
||||||
|
}
|
||||||
|
/// <summary>
|
||||||
|
/// 查找元素并绑定命令
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="methodName"></param>
|
||||||
|
/// <param name="find"></param>
|
||||||
|
/// <param name="ps"></param>
|
||||||
|
public CommandDescribe(string methodName, Func<UIElement, UIElement> find, params object[] ps)
|
||||||
|
{
|
||||||
|
MethodName = methodName;
|
||||||
|
Parameters = ps;
|
||||||
|
Find = find;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,11 @@ using System;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using System.ComponentModel;
|
||||||
|
|
||||||
namespace CPF.Controls
|
namespace CPF.Controls
|
||||||
{
|
{
|
||||||
|
[Browsable(false)]
|
||||||
public class LineNumber : Control
|
public class LineNumber : Control
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -193,7 +193,21 @@ namespace CPF
|
|||||||
{
|
{
|
||||||
if (value.Command != null)
|
if (value.Command != null)
|
||||||
{
|
{
|
||||||
Commands.Add(propertyName, value.Command);
|
if (value.Command.Action != null)
|
||||||
|
{
|
||||||
|
Commands.Add(propertyName, value.Command.Action);
|
||||||
|
}
|
||||||
|
else if (!string.IsNullOrWhiteSpace(value.Command.MethodName))
|
||||||
|
{
|
||||||
|
if (value.Command.Find != null)
|
||||||
|
{
|
||||||
|
Commands.Add(propertyName, value.Command.MethodName, value.Command.Find, value.Command.Parameters);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Commands.Add(propertyName, value.Command.MethodName, value.Command.Target, value.Command.Parameters);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@ -231,10 +245,22 @@ namespace CPF
|
|||||||
{
|
{
|
||||||
var type = attached.GetType();
|
var type = attached.GetType();
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
if (value is AttachedDescribe describe)
|
||||||
|
{
|
||||||
|
var p = typeof(OptionalParameter<>).MakeGenericType(type.GetGenericArguments()[0]);
|
||||||
|
attached.Method.FastInvoke(attached.Target, this, Activator.CreateInstance(p, describe.Value));
|
||||||
|
var targetType = attached.Target.GetType();
|
||||||
|
var field = targetType.GetField("propertyName");
|
||||||
|
var name = field.FastGetValue(attached.Target).ToString();
|
||||||
|
AttachedNotify.Bindings.Add(name, describe.PropertyName, describe.Source, describe.BindingMode, describe.Convert, describe.ConvertBack, describe.SourceToTargetError, describe.TargetToSourceError);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
var p = typeof(OptionalParameter<>).MakeGenericType(type.GetGenericArguments()[0]);
|
var p = typeof(OptionalParameter<>).MakeGenericType(type.GetGenericArguments()[0]);
|
||||||
attached.Method.FastInvoke(attached.Target, this, Activator.CreateInstance(p, value));
|
attached.Method.FastInvoke(attached.Target, this, Activator.CreateInstance(p, value));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
if (type.GetGenericTypeDefinition() != typeof(Attached<>))
|
if (type.GetGenericTypeDefinition() != typeof(Attached<>))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user