Merge remote-tracking branch 'origin/master'

This commit is contained in:
小红帽 2023-11-28 12:50:58 +08:00
commit 91cb1fb2d7
4 changed files with 112 additions and 14 deletions

View File

@ -237,6 +237,14 @@ namespace CPF
internal set; internal set;
} }
/// <summary> /// <summary>
/// 链式绑定下标
/// </summary>
public int SourcePropertyIndex
{
get;
internal set;
} = 0;
/// <summary>
/// Owner被绑定的属性名 /// Owner被绑定的属性名
/// </summary> /// </summary>
public string TargetPropertyName public string TargetPropertyName
@ -354,6 +362,7 @@ namespace CPF
current.Push(this); current.Push(this);
try try
{ {
if (Source == null || !Source.IsAlive) if (Source == null || !Source.IsAlive)
{ {
if (Owner.HasProperty(TargetPropertyName)) if (Owner.HasProperty(TargetPropertyName))
@ -380,8 +389,9 @@ namespace CPF
value = Source.Target.GetPropretyValue(SourcePropertyName); value = Source.Target.GetPropretyValue(SourcePropertyName);
}*/ }*/
value = GetPropertySource(SourcePropertyName, Source.Target); value = GetPropertySource(SourcePropertyName, Source.Target);
value = value.GetPropretyValue(SourcePropertyName.Split('.').LastOrDefault()); if (value != null) {
value = value.GetPropretyValue(SourcePropertyName.Split('.').LastOrDefault());
}
if (Convert != null) if (Convert != null)
{ {
value = Convert(value); value = Convert(value);
@ -446,17 +456,22 @@ namespace CPF
{ {
if (!b.SetValue(nv, SourcePropertyName)) if (!b.SetValue(nv, SourcePropertyName))
{ {
var SourcePropertyNames = SourcePropertyName.Split('.'); /*var SourcePropertyNames = SourcePropertyName.Split('.');
if (SourcePropertyNames.Length == 1) if (SourcePropertyNames.Length == 1)
{ {
b.SetValue(SourcePropertyName, nv); b.SetValue(SourcePropertyName, nv);
} }*/
var Target = b; /*var Target = b;
for (int i = 0; i < SourcePropertyNames.Length-1; i++) for (int i = 0; i < SourcePropertyNames.Length-1; i++)
{ {
Target = Target.GetPropretyValue(SourcePropertyNames[i]) as CpfObject; 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); //b.Type.GetProperty(SourcePropertyName).FastSetValue(b, nv);
} }
} }
@ -573,7 +588,21 @@ namespace CPF
var Target = Source; var Target = Source;
for (int i = 0; i < SourcePropertyNames.Length - 1; i++) 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; return Target;
} }
@ -582,6 +611,17 @@ namespace CPF
throw new Exception("错误:{ex}"); 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) internal void RegisterPropertyChanged(INotifyPropertyChanged notify)
{ {
@ -589,7 +629,12 @@ namespace CPF
//{ //{
// throw new Exception("错误"); // 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); RegisterPropertyChanged(notifySource as INotifyPropertyChanged, PropertyChanged);
} }
internal void CancellationPropertyChanged(INotifyPropertyChanged notify) internal void CancellationPropertyChanged(INotifyPropertyChanged notify)

View File

@ -110,6 +110,7 @@
<PropertyGroup> <PropertyGroup>
<DefineConstants Condition="'$(TargetFramework)'=='net40'">Net4</DefineConstants> <DefineConstants Condition="'$(TargetFramework)'=='net40'">Net4</DefineConstants>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
<Nullable>disable</Nullable>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp3.0|AnyCPU'"> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|netcoreapp3.0|AnyCPU'">
<Optimize>false</Optimize> <Optimize>false</Optimize>

View File

@ -23,7 +23,37 @@ namespace ConsoleApp1
{ {
test = this; 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 public string Name
{ {
get get
@ -38,10 +68,10 @@ namespace ConsoleApp1
} }
class MainModel : CpfObject class MainModel : CpfObject
{ {
public data Test1 { public dataa Test1 {
get get
{ {
return (data)GetValue(); return (dataa)GetValue();
} }
set set
{ {
@ -68,7 +98,7 @@ namespace ConsoleApp1
public MainModel() public MainModel()
{ {
Test1 = new data(); Test1 = new dataa();
Items = new Collection<(string, string)>(); Items = new Collection<(string, string)>();
TestItems = new Collection<(string, int)>(); TestItems = new Collection<(string, int)>();

View File

@ -2454,10 +2454,32 @@ new TabItemTemplate{
}, },
new Button new Button
{ {
Content="按钮", Content="创建对象",
[nameof(Button.Click)]=new CommandDescribe((s,e)=> [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;
}) })
}, },
} }