Copy DependencyProperty with Reflection


  1.    10 Jul 2007 #1
    Horst Klein Guest

    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

  2.    10 Jul 2007 #2
    Lloyd Dupont Guest

    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

  3.    10 Jul 2007 #3
    Horst Klein Guest

    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

Copy DependencyProperty with Reflection

Similar Threads
Thread Forum
reflection, COM, and Vista
Hi all, I've been struggling with an issue that I hope you can comment on or provide suggestions to. Our .NET 2.0 (VS2005) based product is...
.NET General
Design time only DependencyProperty
Hi, I'm creating a user control in WPF, and I would like to have a property which can only be set in design time. I'm using this property as a...
Avalon
No notification when a DependencyProperty value has changed?
I am working on a FrameworkElement called Plot which has to dynamically create TextBlocks. All of them should use the same font size, font family,...
Avalon
obtain type of dependencyproperty
When I register a DepencyPorperty I can specify what type it is supposed to store. How can I find out what type is set for a DepenencyProperty of a...
Avalon
C++/CLI based DependencyProperty example
Is there any examples of showing how one might add a DependencyProperty to a C++ class? All of the examples I've found are C# based and I'm not...
Avalon
How to bind a Trigger to a user defined DependencyProperty
Hi I do the following inside my code public partial class ShoppingCart : StackPanel, INotifyPropertyChanged { public ShoppingCart() {...
Avalon
Re: reflection with a viewport3D
Hi Adam, thanks for the reply. I'm a newb with 3d, so to quickly summarize your last post: Two main ways to achieve mirror 3d effects: 1) Add...
Avalon

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 11:36.
    .