Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:17:10+00:00 2026-05-23T03:17:10+00:00

Can anybody give me an example how to implement AttachedCommands? There are some examples

  • 0

Can anybody give me an example how to implement AttachedCommands?

There are some examples in C# and I think it is very similar to C# but I have problems to translate the code to VB.NET.

At the moment my try to translate a C# AttachedCommand-Class to VB.net looks like that:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Input
Imports System.Reflection

Public Class AttachedCommand
    Inherits DependencyObject

#Region "CommandParameter-Class"

    Private NotInheritable Class CommandParameter

    Public Shared ReadOnly Property DefaultCP() As CommandParameter
        Get
            Return New CommandParameter()
        End Get
    End Property



    Private _command As ICommand
    Public Property Command() As ICommand
        Get
            Return _command
        End Get
        Set(ByVal value As ICommand)
            _command = value
        End Set
    End Property


    Private _eventName As String
    Public Property EventName() As String
        Get
            Return _eventName
        End Get
        Set(ByVal value As String)
            _eventName = value
        End Set
    End Property


    Private _callBack As [Delegate]
    Public Property Callback() As [Delegate]
        Get
            Return _callBack
        End Get
        Set(ByVal value As [Delegate])
            _callBack = value
        End Set
    End Property

End Class

#End Region

#Region "Properties"

Private Shared _parameter As IDictionary(Of DependencyObject, CommandParameter)
Private Shared Property Parameter() As IDictionary(Of DependencyObject, CommandParameter)
    Get
        Return _parameter
    End Get
    Set(ByVal value As IDictionary(Of DependencyObject, CommandParameter))
        _parameter = value
    End Set
End Property




Public Property Command() As ICommand
    Get
        Return GetValue(CommandProperty)
    End Get

    Set(ByVal value As ICommand)
        SetValue(CommandProperty, value)
    End Set
End Property

Public Shared ReadOnly CommandProperty As DependencyProperty = _
                       DependencyProperty.Register("Command", _
                       GetType(ICommand), GetType(AttachedCommand), _
                       New UIPropertyMetadata(AddressOf CommandChanged))

Public Property EventName() As String
    Get
        Return GetValue(EventNameProperty)
    End Get

    Set(ByVal value As String)
        SetValue(EventNameProperty, value)
    End Set
End Property

Public Shared ReadOnly EventNameProperty As DependencyProperty = _
                       DependencyProperty.Register("EventName", _
                       GetType(String), GetType(AttachedCommand), _
                       New UIPropertyMetadata(AddressOf EventNameChanged))


#End Region

#Region "Event-Handler"

Public Shared Sub CommandChanged(ByVal dependencyObject As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
    If Not Parameter.ContainsKey(dependencyObject) Then
        Parameter.Add(dependencyObject, CommandParameter.DefaultCP)
    End If

    If (Not Parameter(dependencyObject).Callback = Nothing) AndAlso (Not args.OldValue = Nothing) Then
        _RemoveEventHandler(dependencyObject)
    End If

    Parameter(dependencyObject).Command = CType(args.NewValue, ICommand)

    _AttachEventHandler(dependencyObject)

End Sub

Public Shared Sub EventNameChanged(ByVal dependencyObject As DependencyObject, ByVal args As DependencyPropertyChangedEventArgs)
    If Not Parameter.ContainsKey(dependencyObject) Then
        Parameter.Add(dependencyObject, CommandParameter.DefaultCP)
    End If
End Sub

#End Region


#Region "Helper"

Private Shared Sub _RemoveEventHandler(ByVal dependencyObject As DependencyObject)
    If dependencyObject Is Nothing Then
        Throw New ArgumentNullException("DependencyObject is null.")
    End If

    If Not Parameter.ContainsKey(dependencyObject) Then
        Exit Sub
    End If

    Dim param As CommandParameter = Parameter(dependencyObject)

    If param.Callback Is Nothing Then
        Throw New InvalidProgramException("Cannot remove Callback. Callback is null.")
    End If

    Dim eventInfo As EventInfo = dependencyObject.GetType().GetEvent(param.EventName)
    If eventInfo Is Nothing Then
        Throw New InvalidProgramException(String.Format("Cannot find an event with the name <{0}>", param.EventName))
    End If

    eventInfo.RemoveEventHandler(dependencyObject, param.Callback)

End Sub

Private Shared Sub _AttachEventHandler(ByVal dependencyObject)
    If dependencyObject Is Nothing Then
        Throw New ArgumentNullException("DependencyObject is null")
    End If

    If Not Parameter.ContainsKey(dependencyObject) Then
        Exit Sub
    End If

    Dim param As CommandParameter = Parameter(dependencyObject)

    If param.Command Is Nothing Or String.IsNullOrEmpty(param.EventName) Then
        Exit Sub
    End If

    Dim eventInfo As EventInfo = dependencyObject.GetType.GetEvent(param.EventName)
    If eventInfo Is Nothing Then
        Throw New InvalidProgramException(String.Format("Cannot find an event with the name <{0}>", param.EventName))
    End If



    eventInfo.AddEventHandler(dependencyObject, param.Callback)

End Sub

Private Shared Sub _CommandExecutAction(ByVal parameter As CommandParameter)
    parameter.Command.Execute(Nothing)
End Sub

Private Shared Function _CreateHandler(ByVal eventInfo As EventInfo, ByVal method As Action) As [Delegate]
    If eventInfo Is Nothing Then
        Throw New ArgumentNullException("EventInfo is null")
    End If

    If method Is Nothing Then
        Throw New ArgumentNullException("Action-method is null")
    End If

    Dim handlerType = eventInfo.EventHandlerType
    Dim eventParams = handlerType.GetMethod("Invoke").GetParameters()

    Dim parameters = eventParams.[Select](Function(p) System.Linq.Expressions.Expression.Parameter(p.ParameterType, "x"))
    Dim body = System.Linq.Expressions.Expression.[Call](System.Linq.Expressions.Expression.Constant(method), method.[GetType]().GetMethod("Invoke"))
    Dim lambda = System.Linq.Expressions.Expression.Lambda(body, parameters.ToArray())

    Return [Delegate].CreateDelegate(handlerType, lambda.Compile(), "Invoke", False)

End Function
#End Region

#Region "INIT"

Public Sub New()
    Parameter = New Dictionary(Of DependencyObject, CommandParameter)()
End Sub

#End Region

End Class

But now I have the problem, that the GetValue() and SetValue()-methods are not available. Any ideas?

Thank you for your help..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:17:10+00:00Added an answer on May 23, 2026 at 3:17 am

    An AttachedCommandBehaviour ain’t much more than an dependency property of type ICommand, which hookes some events.

    Check out Dr. WPFs VB-snippets that includes snippets for dependency properties.

    If you use the one that includes a property changed handler, you can use this handler to hook the events you want. In the event handler, you call the attached command.

    If you still can’t make it work, please post some code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody point to/give a very very correct example for the Model-View-Controller paradigm? I
Can anybody give an example of c++ code that can easily convert a decimal
Can anybody give me an example how to use the osgi framework classes? I
Can anybody give me an example how to load the module? For example if
Can anybody give me an example when to use allow null default 0 default
Can anybody give me an example how I would download an image asynchroniously and
Can anybody give a clear explanation of how variable assignment really works in Makefiles.
can anybody recommend some really good resources for how to get Apache authenticating users
Can anybody recommend a reliable and decently documented code highlighter for WordPress 2.6.1? I
Can anybody recommend a good code profiler for C++? I came across Shiny -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.