Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
T
Thick-Common
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
潘栩锋
Thick-Common
Commits
0c240d5f
Commit
0c240d5f
authored
May 12, 2023
by
潘栩锋
🚴
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
修复 属性绑定,添加卸载功能
parent
5207ee25
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
154 additions
and
21 deletions
+154
-21
PropertyBinding.cs
Project.FLY.Misc/MISC/PropertyBinding.cs
+154
-21
No files found.
Project.FLY.Misc/MISC/PropertyBinding.cs
View file @
0c240d5f
...
...
@@ -9,7 +9,7 @@ namespace Misc
{
public
class
BindingOperations
{
// 摘要:
// 描述绑定中数据流的方向。
public
enum
BindingMode
...
...
@@ -31,11 +31,11 @@ namespace Misc
/// <param name="srcPropertyName"></param>
/// <param name="target"></param>
/// <param name="targetPropertyName"></param>
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
INotifyPropertyChanged
target
,
string
targetPropertyName
)
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
INotifyPropertyChanged
target
,
string
targetPropertyName
)
{
SetBinding
(
src
,
srcPropertyName
,
target
,
targetPropertyName
,
BindingMode
.
OneWay
);
}
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
IList
target
,
int
index
)
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
IList
target
,
int
index
)
{
Type
type_s
=
src
.
GetType
();
...
...
@@ -43,8 +43,8 @@ namespace Misc
System
.
Reflection
.
PropertyInfo
pi_s
=
type_s
.
GetProperty
(
srcPropertyName
);
if
(
pi_s
==
null
)
return
;
object
obj
=
pi_s
.
GetValue
(
src
,
null
);
object
obj
=
pi_s
.
GetValue
(
src
,
null
);
target
[
index
]
=
obj
;
src
.
PropertyChanged
+=
(
s
,
e
)
=>
...
...
@@ -55,31 +55,130 @@ namespace Misc
target
[
index
]
=
obj
;
}
};
}
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
Action
func
)
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
Action
func
)
{
func
();
src
.
PropertyChanged
+=
(
s
,
e
)
=>
{
if
(
srcPropertyName
==
e
.
PropertyName
)
if
(
srcPropertyName
==
e
.
PropertyName
)
{
func
();
}
};
}
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
[]
srcPropertyNames
,
Action
func
)
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
[]
srcPropertyNames
,
Action
func
)
{
func
();
src
.
PropertyChanged
+=
(
s
,
e
)
=>
{
if
(
srcPropertyNames
.
Contains
(
e
.
PropertyName
))
if
(
srcPropertyNames
.
Contains
(
e
.
PropertyName
))
{
func
();
}
};
}
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
INotifyPropertyChanged
target
,
string
targetPropertyName
,
BindingMode
mode
)
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
object
target
,
Action
func
)
{
func
();
PropertyChangedEventHandler
pcevent
;
src
.
PropertyChanged
+=
pcevent
=
(
s
,
e
)
=>
{
if
(
srcPropertyName
==
e
.
PropertyName
)
{
func
();
}
};
if
(
IsMarkdownEvents
)
{
if
(!
bindingConexts
.
ContainsKey
(
target
))
{
bindingConexts
.
Add
(
target
,
new
List
<
PropertyChangedEventContexts
>());
}
var
list
=
bindingConexts
[
target
];
list
.
Add
(
new
PropertyChangedEventContexts
()
{
src
=
src
,
propertyChanged
=
pcevent
});
}
}
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
[]
srcPropertyNames
,
object
target
,
Action
func
)
{
func
();
PropertyChangedEventHandler
pcevent
;
src
.
PropertyChanged
+=
pcevent
=
(
s
,
e
)
=>
{
if
(
srcPropertyNames
.
Contains
(
e
.
PropertyName
))
{
func
();
}
};
if
(
IsMarkdownEvents
)
{
if
(!
bindingConexts
.
ContainsKey
(
target
))
{
bindingConexts
.
Add
(
target
,
new
List
<
PropertyChangedEventContexts
>());
}
var
list
=
bindingConexts
[
target
];
list
.
Add
(
new
PropertyChangedEventContexts
()
{
src
=
src
,
propertyChanged
=
pcevent
});
}
}
public
class
PropertyChangedEventContexts
{
public
INotifyPropertyChanged
src
;
public
PropertyChangedEventHandler
propertyChanged
;
}
static
Dictionary
<
object
,
List
<
PropertyChangedEventContexts
>>
bindingConexts
;
static
bool
IsMarkdownEvents
=
false
;
/// <summary>
/// 开始记录事件
/// </summary>
public
static
void
StartMarkdownEvents
(
Dictionary
<
object
,
List
<
PropertyChangedEventContexts
>>
bindingConexts
)
{
IsMarkdownEvents
=
true
;
BindingOperations
.
bindingConexts
=
bindingConexts
;
}
/// <summary>
/// 停止记录事件
/// </summary>
public
static
void
StopMarkdownEvents
()
{
IsMarkdownEvents
=
false
;
BindingOperations
.
bindingConexts
=
null
;
}
/// <summary>
/// 释放事件目标对象
/// </summary>
/// <param name="src"></param>
public
static
void
DisposeEventTargetObject
(
Dictionary
<
object
,
List
<
PropertyChangedEventContexts
>>
bindingConexts
,
object
target
)
{
if
(!
bindingConexts
.
ContainsKey
(
target
))
return
;
var
list
=
bindingConexts
[
target
];
foreach
(
var
context
in
list
)
{
context
.
src
.
PropertyChanged
-=
context
.
propertyChanged
;
}
bindingConexts
.
Remove
(
target
);
}
/// <summary>
/// 释放事件目标对象
/// </summary>
/// <param name="src"></param>
public
static
void
DisposeEventTargetObject
(
Dictionary
<
object
,
List
<
PropertyChangedEventContexts
>>
bindingConexts
)
{
foreach
(
var
list
in
bindingConexts
.
Values
)
{
foreach
(
var
context
in
list
)
{
context
.
src
.
PropertyChanged
-=
context
.
propertyChanged
;
}
}
bindingConexts
.
Clear
();
}
public
static
void
SetBinding
(
INotifyPropertyChanged
src
,
string
srcPropertyName
,
INotifyPropertyChanged
target
,
string
targetPropertyName
,
BindingMode
mode
)
{
Type
type_s
=
src
.
GetType
();
Type
type_t
=
target
.
GetType
();
...
...
@@ -87,15 +186,16 @@ namespace Misc
System
.
Reflection
.
PropertyInfo
pi_t
=
type_t
.
GetProperty
(
targetPropertyName
);
if
(
pi_s
==
null
||
pi_t
==
null
)
return
;
switch
(
mode
)
switch
(
mode
)
{
case
BindingMode
.
OneWay
:
//src->target
{
object
obj
=
pi_s
.
GetValue
(
src
,
null
);
object
obj
=
pi_s
.
GetValue
(
src
,
null
);
pi_t
.
SetValue
(
target
,
obj
,
null
);
src
.
PropertyChanged
+=
(
s
,
e
)
=>
PropertyChangedEventHandler
pcevent
;
src
.
PropertyChanged
+=
pcevent
=
(
s
,
e
)
=>
{
if
(
e
.
PropertyName
==
srcPropertyName
)
{
...
...
@@ -103,13 +203,25 @@ namespace Misc
pi_t
.
SetValue
(
target
,
o
,
null
);
}
};
}
break
;
if
(
IsMarkdownEvents
)
{
if
(!
bindingConexts
.
ContainsKey
(
target
))
{
bindingConexts
.
Add
(
target
,
new
List
<
PropertyChangedEventContexts
>());
}
var
list
=
bindingConexts
[
target
];
list
.
Add
(
new
PropertyChangedEventContexts
()
{
src
=
src
,
propertyChanged
=
pcevent
});
}
}
break
;
case
BindingMode
.
TwoWay
:
//src->target then target->src
{
object
obj
=
pi_s
.
GetValue
(
src
,
null
);
object
obj
=
pi_s
.
GetValue
(
src
,
null
);
pi_t
.
SetValue
(
target
,
obj
,
null
);
src
.
PropertyChanged
+=
(
s
,
e
)
=>
PropertyChangedEventHandler
pcevent
;
src
.
PropertyChanged
+=
pcevent
=
(
s
,
e
)
=>
{
if
(
e
.
PropertyName
==
srcPropertyName
)
{
...
...
@@ -118,7 +230,17 @@ namespace Misc
}
};
target
.
PropertyChanged
+=
(
s
,
e
)
=>
if
(
IsMarkdownEvents
)
{
if
(!
bindingConexts
.
ContainsKey
(
target
))
{
bindingConexts
.
Add
(
target
,
new
List
<
PropertyChangedEventContexts
>());
}
var
list
=
bindingConexts
[
target
];
list
.
Add
(
new
PropertyChangedEventContexts
()
{
src
=
src
,
propertyChanged
=
pcevent
});
}
target
.
PropertyChanged
+=
pcevent
=
(
s
,
e
)
=>
{
if
(
e
.
PropertyName
==
targetPropertyName
)
{
...
...
@@ -126,7 +248,18 @@ namespace Misc
pi_s
.
SetValue
(
src
,
o
,
null
);
}
};
}
break
;
if
(
IsMarkdownEvents
)
{
if
(!
bindingConexts
.
ContainsKey
(
src
))
{
bindingConexts
.
Add
(
src
,
new
List
<
PropertyChangedEventContexts
>());
}
var
list
=
bindingConexts
[
src
];
list
.
Add
(
new
PropertyChangedEventContexts
()
{
src
=
target
,
propertyChanged
=
pcevent
});
}
}
break
;
}
}
...
...
@@ -143,7 +276,7 @@ namespace Misc
{
object
obj
=
pi_s
.
GetValue
(
src
,
null
);
target
.
SetValue
(
targetPropertyName
,
obj
);
src
.
PropertyChanged
+=
(
s
,
e
)
=>
{
if
(
e
.
PropertyName
==
srcPropertyName
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment