diff --git a/CPF/AttachedProperties.cs b/CPF/AttachedProperties.cs index 67715b5..a0e2988 100644 --- a/CPF/AttachedProperties.cs +++ b/CPF/AttachedProperties.cs @@ -212,4 +212,104 @@ namespace CPF return false; } } + /// + /// 绑定附加属性 + /// + public class AttachedDescribe : BindingDescribe + { + /// + /// 设置和绑定附加属性 + /// + /// + /// + public AttachedDescribe(object value, string sourceProperty) : base(sourceProperty) + { + Value = value; + } + /// + /// 设置和绑定附加属性 + /// + /// + /// + /// + public AttachedDescribe(object value, string sourceProperty, BindingMode binding) : base(sourceProperty, binding) + { + Value = value; + } + /// + /// 设置和绑定附加属性 + /// + /// + /// + /// + /// + public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding) : base(source, sourceProperty, binding) + { + Value = value; + } + /// + /// 设置和绑定附加属性 + /// + /// + /// + /// + public AttachedDescribe(object value, object source, string sourceProperty) : base(source, sourceProperty) + { + Value = value; + } + /// + /// 设置和绑定附加属性 + /// + /// + /// + /// + /// + public AttachedDescribe(object value, object source, string sourceProperty, Func convert) : base(source, sourceProperty, convert) + { + Value = value; + } + /// + /// 设置和绑定附加属性 + /// + /// + /// + /// + /// + /// + public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding, Func convert) : base(source, sourceProperty, binding, convert) + { + Value = value; + } + /// + /// 设置和绑定附加属性 + /// + /// + /// + /// + /// + /// + /// + public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding, Func convert, Func convertBack) : base(source, sourceProperty, binding, convert, convertBack) + { + Value = value; + } + /// + /// 设置和绑定附加属性 + /// + /// + /// + /// + /// + /// + /// + /// + /// + public AttachedDescribe(object value, object source, string sourceProperty, BindingMode binding, Func convert, Func convertBack, Action SourceToTargetError, Action TargetToSourceError) : base(source, sourceProperty, binding, convert, convertBack, SourceToTargetError, TargetToSourceError) + { + Value = value; + } + + + public object Value { get; set; } + } } diff --git a/CPF/BindingDescribe.cs b/CPF/BindingDescribe.cs index 75585a0..1b807e3 100644 --- a/CPF/BindingDescribe.cs +++ b/CPF/BindingDescribe.cs @@ -64,7 +64,7 @@ namespace CPF this.TargetToSourceError = TargetToSourceError; } - public BindingDescribe(Action command) + public BindingDescribe(CommandDescribe command) { Command = command; } @@ -78,7 +78,7 @@ namespace CPF /// /// 简化绑定命令的命令,如果设置了该属性,则使用命令绑定 /// - public Action Command { get; set; } + public CommandDescribe Command { get; set; } //public CpfObject Owner { get; internal set; } @@ -349,7 +349,7 @@ namespace CPF { return new BindingDescribe { PropertyName = sourceProperty }; } - public static explicit operator BindingDescribe(Action command) + public static implicit operator BindingDescribe(CommandDescribe command) { return new BindingDescribe { Command = command }; } @@ -392,5 +392,50 @@ namespace CPF return base.GetHashCode(); } } + /// + /// 命令绑定 + /// + public class CommandDescribe + { + public Action Action { get; set; } + public string MethodName { get; set; } + public object[] Parameters { get; set; } + + public object Target { get; set; } + + public Func Find { get; set; } + /// + /// 用委托定义个命令绑定 + /// + /// + public CommandDescribe(Action command) + { + Action = command; + } + /// + /// 定义个命令绑定 + /// + /// + /// + /// + public CommandDescribe(string methodName, object obj = null, params object[] ps) + { + MethodName = methodName; + Parameters = ps; + Target = obj; + } + /// + /// 查找元素并绑定命令 + /// + /// + /// + /// + public CommandDescribe(string methodName, Func find, params object[] ps) + { + MethodName = methodName; + Parameters = ps; + Find = find; + } + } } diff --git a/CPF/Controls/CodeTextBox/LineNumber.cs b/CPF/Controls/CodeTextBox/LineNumber.cs index 2860452..ffb210b 100644 --- a/CPF/Controls/CodeTextBox/LineNumber.cs +++ b/CPF/Controls/CodeTextBox/LineNumber.cs @@ -11,9 +11,11 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.ComponentModel; namespace CPF.Controls { + [Browsable(false)] public class LineNumber : Control { diff --git a/CPF/CpfObject.cs b/CPF/CpfObject.cs index 0c850cf..b562a19 100644 --- a/CPF/CpfObject.cs +++ b/CPF/CpfObject.cs @@ -193,7 +193,21 @@ namespace CPF { 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 { @@ -232,8 +246,20 @@ namespace CPF var type = attached.GetType(); try { - var p = typeof(OptionalParameter<>).MakeGenericType(type.GetGenericArguments()[0]); - attached.Method.FastInvoke(attached.Target, this, Activator.CreateInstance(p, value)); + 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]); + attached.Method.FastInvoke(attached.Target, this, Activator.CreateInstance(p, value)); + } } catch (Exception e) {