Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

Vista - Copy DependencyProperty with Reflection

 
 
Old 07-10-2007   #1 (permalink)
Horst Klein


 
 

Copy DependencyProperty with Reflection

I need to copy (clone) Properties of an FrameworkElement.
For the normal properties it's simple.

But how can I find and copy the DependencyProperties?

Thanks Horst

My System SpecsSystem Spec
Old 07-10-2007   #2 (permalink)
Lloyd Dupont


 
 

Re: Copy DependencyProperty with Reflection

you need to give more detail if you hope to receive a useful answer.

meanwhile you can be interested in the following method:
DependencyObject.ReadLocalValue()


"Horst Klein" <HorstKlein@discussions.microsoft.com> wrote in message
news:B73B9C94-C54C-4792-A24C-94806380B41D@microsoft.com...
>I need to copy (clone) Properties of an FrameworkElement.
> For the normal properties it's simple.
>
> But how can I find and copy the DependencyProperties?
>
> Thanks Horst



My System SpecsSystem Spec
Old 07-10-2007   #3 (permalink)
Horst Klein


 
 

Re: Copy DependencyProperty with Reflection

Hi Lloyd

To copy (clone) my standard properties i use this Method:

In the selectedProperties Argument I can define wich properties i loke to
copy.


Private Shared Sub CloneSelectedProperties(ByVal cloneElement As Object,
ByVal originElement As Object, ByVal selectedProperties As ArrayList)
Dim originProperty As Reflection.PropertyInfo
Dim cloneProperty As Reflection.PropertyInfo
Dim propertyName As String = ""
For Each propertyName In selectedProperties
originProperty = originElement.GetType.GetProperty(propertyName)
cloneProperty = cloneElement.GetType.GetProperty(propertyName)
If originProperty IsNot Nothing Then
If cloneProperty.CanWrite Then
cloneProperty.SetValue(cloneElement,
originProperty.GetValue(originElement, Nothing), Nothing)
Else
Dim reasonOfException As String =
VitoApplication.GetText(Core.TextCodes.PropertyIsReadOnly)
Dim objectInfo As String = originElement.GetType.FullName
Dim nameProperty As Reflection.PropertyInfo =
originElement.GetType.GetProperty("Name")
If nameProperty IsNot Nothing Then
objectInfo = objectInfo & " (Name=" &
nameProperty.GetValue(originElement, Nothing).ToString & ") "
End If
reasonOfException = String.Format(reasonOfException,
propertyName, objectInfo)
Throw New ElementMergeException("CloneSelectedProperties",
reasonOfException)
End If
End If
Next
End Sub

To Copy the Attached Properties i use this function

Private Shared Sub CloneDependencyProperties(ByVal cloneElement As
Object, ByVal originElement As Object)
Dim originDepObj As DependencyObject = TryCast(originElement,
DependencyObject)
If originDepObj IsNot Nothing Then
CloneDependencyProperty(CustomEventCommandBindingProperty,
originDepObj, cloneElement)
CloneDependencyProperty(DefaultEventCommandBindingProperty,
originDepObj, cloneElement)
End If
End Sub

Private Shared Sub CloneDependencyProperty(ByVal dependencyProperty As
DependencyProperty, ByVal originElement As DependencyObject, ByVal
cloneElement As Object)
Dim originDepObj As DependencyObject = TryCast(originElement,
DependencyObject)
Dim cloneDepProperty As Object
If originDepObj IsNot Nothing Then
cloneDepProperty = originDepObj.GetValue(dependencyProperty)
If cloneDepProperty IsNot Nothing Then
Dim cloneDepObj As DependencyObject = DirectCast(cloneElement,
DependencyObject)
cloneDepObj.SetValue(CustomEventCommandBindingProperty,
cloneDepProperty)
End If
End If
End Sub


It works, but I like to have a more generic solution

Horst
My System SpecsSystem Spec
 

Thread Tools



Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46