diff --git a/CPF/Binding.cs b/CPF/Binding.cs index 9ebc15b..18de00e 100644 --- a/CPF/Binding.cs +++ b/CPF/Binding.cs @@ -237,6 +237,14 @@ namespace CPF internal set; } /// + /// 链式绑定下标 + /// + public int SourcePropertyIndex + { + get; + internal set; + } = 0; + /// /// Owner被绑定的属性名 /// public string TargetPropertyName @@ -354,6 +362,7 @@ namespace CPF current.Push(this); try { + if (Source == null || !Source.IsAlive) { if (Owner.HasProperty(TargetPropertyName)) @@ -380,8 +389,9 @@ namespace CPF value = Source.Target.GetPropretyValue(SourcePropertyName); }*/ value = GetPropertySource(SourcePropertyName, Source.Target); - value = value.GetPropretyValue(SourcePropertyName.Split('.').LastOrDefault()); - + if (value != null) { + value = value.GetPropretyValue(SourcePropertyName.Split('.').LastOrDefault()); + } if (Convert != null) { value = Convert(value); @@ -446,17 +456,22 @@ namespace CPF { if (!b.SetValue(nv, SourcePropertyName)) { - var SourcePropertyNames = SourcePropertyName.Split('.'); + /*var SourcePropertyNames = SourcePropertyName.Split('.'); if (SourcePropertyNames.Length == 1) { b.SetValue(SourcePropertyName, nv); - } - var Target = b; + }*/ + /*var Target = b; for (int i = 0; i < SourcePropertyNames.Length-1; i++) { Target = Target.GetPropretyValue(SourcePropertyNames[i]) as CpfObject; + }*/ + var SourcePropertyNames = SourcePropertyName.Split('.'); + var Target = GetPropertySource(SourcePropertyName, b); + if (Target != null) { + Target.SetValue(SourcePropertyNames.LastOrDefault(), nv); } - Target.SetValue(nv, SourcePropertyNames.LastOrDefault()); + //b.Type.GetProperty(SourcePropertyName).FastSetValue(b, nv); } } @@ -573,7 +588,21 @@ namespace CPF var Target = Source; for (int i = 0; i < SourcePropertyNames.Length - 1; i++) { - Target = Target.GetPropretyValue(SourcePropertyNames[i]) as CpfObject; + var Temp_Target = Target.GetPropretyValue(SourcePropertyNames[i]) as CpfObject; + if (Temp_Target == null) + { + //如果链式绑定的子级为null,那么监听他的父级监听子级 + this.SourcePropertyIndex = i; + if (SourceProperty!=null) + { + SourceProperty.PropertyChanged -= Target_PropertyChanged; + CancellationPropertyChanged(SourceProperty); + } + (Target as CpfObject).PropertyChanged += Target_PropertyChanged; + SourceProperty = (Target as CpfObject); + return null; + } + Target = Temp_Target; } return Target; } @@ -582,6 +611,17 @@ namespace CPF throw new Exception("错误:{ex}"); } } + CpfObject SourceProperty = null; + private void Target_PropertyChanged(object sender, CPFPropertyChangedEventArgs e) + { + if (e.NewValue == null) + { + CancellationPropertyChanged(sender as CpfObject); + return; + } + + RegisterPropertyChanged(sender as CpfObject); + } internal void RegisterPropertyChanged(INotifyPropertyChanged notify) { @@ -589,7 +629,12 @@ namespace CPF //{ // throw new Exception("错误"); //} - var notifySource = GetPropertySource(this.SourcePropertyName, notify); + var Temp_SourcePropertyName = string.Join(".", this.SourcePropertyName.Split('.').Skip(this.SourcePropertyIndex)); + var notifySource = GetPropertySource(Temp_SourcePropertyName, notify); + if (notifySource == null) + { + return; + } RegisterPropertyChanged(notifySource as INotifyPropertyChanged, PropertyChanged); } internal void CancellationPropertyChanged(INotifyPropertyChanged notify) diff --git a/ConsoleApp1/ConsoleApp1.csproj b/ConsoleApp1/ConsoleApp1.csproj index 29ad6f2..0967888 100644 --- a/ConsoleApp1/ConsoleApp1.csproj +++ b/ConsoleApp1/ConsoleApp1.csproj @@ -110,6 +110,7 @@ Net4 false + disable false diff --git a/ConsoleApp1/MainModel.cs b/ConsoleApp1/MainModel.cs index 323879a..48cb9e2 100644 --- a/ConsoleApp1/MainModel.cs +++ b/ConsoleApp1/MainModel.cs @@ -23,7 +23,37 @@ namespace ConsoleApp1 { test = this; } - [PropertyMetadata("我是默认的")] + [PropertyMetadata(null)] + public string Name + { + get + { + return (string)GetValue(); + } + set + { + SetValue(value); + } + } + } + public class dataa : CpfObject + { + public data test + { + get + { + return (data)GetValue(); + } + set + { + SetValue(value); + } + } + public dataa() + { + // test = new data(); + } + [PropertyMetadata(null)] public string Name { get @@ -38,10 +68,10 @@ namespace ConsoleApp1 } class MainModel : CpfObject { - public data Test1 { + public dataa Test1 { get { - return (data)GetValue(); + return (dataa)GetValue(); } set { @@ -68,7 +98,7 @@ namespace ConsoleApp1 public MainModel() { - Test1 = new data(); + Test1 = new dataa(); Items = new Collection<(string, string)>(); TestItems = new Collection<(string, int)>(); diff --git a/ConsoleApp1/Window2.cs b/ConsoleApp1/Window2.cs index c29cc75..c26d849 100644 --- a/ConsoleApp1/Window2.cs +++ b/ConsoleApp1/Window2.cs @@ -2454,10 +2454,32 @@ new TabItemTemplate{ }, new Button { - Content="按钮", + Content="创建对象", [nameof(Button.Click)]=new CommandDescribe((s,e)=> { - (DataContext as MainModel).Test1.test.test.test.test.Name = "666666"; + data a = new data(); + a.test.test.test.Name = "666666"; + (DataContext as MainModel).Test1.test = a; + }) + }, + new Button + { + Content="删除对象", + [nameof(Button.Click)]=new CommandDescribe((s,e)=> + { + data a = new data(); + a.test.test.Name = "666666"; + (DataContext as MainModel).Test1.test.test = null; + }) + }, + new Button + { + Content="添加对象", + [nameof(Button.Click)]=new CommandDescribe((s,e)=> + { + data a = new data(); + a.test.test.Name = "666666"; + (DataContext as MainModel).Test1.test.test = a; }) }, }