Avatar of Ron Kidd
Ron Kidd
Flag for Australia

asked on 

ICommands on Button with Parameter WPF Visual Basic

I have a Button that needs to send a Parameter to a Sub when it is Clicked.

I have managed to get the Command to work WITHOUT a Parameter but can't find how to pass the parameter.

My Relay Class
Imports System.Windows.Input
Public Class RelayCommand
    Implements ICommand

    Private _isEnabled As Boolean
    Private ReadOnly _handler As Action

    Public Sub New(handler As Action)
        _handler = handler
    End Sub

    Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged

    Public Property IsEnabled As Boolean
        Get
            Return _isEnabled
        End Get
        Set(value As Boolean)
            If value <> _isEnabled Then
                _isEnabled = value
                RaiseEvent CanExecuteChanged(Me, EventArgs.Empty)
            End If
        End Set
    End Property

    Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
        Return IsEnabled
    End Function

    Public Sub Execute(parameter As Object) Implements ICommand.Execute
        _handler()
    End Sub

End Class

Open in new window


Welded Class
Imports System.Data.Common
Imports System.Data.SqlClient
Imports System.ComponentModel.DataAnnotations
Public Class ProductionBandsawWelding

    Public Property WeldingComplete As RelayCommand

    Public Sub New()
        SetupCommands()
    End Sub

    Private Sub SetupCommands()
        WeldingComplete = New RelayCommand(AddressOf WeldedNew)
        WeldingComplete.IsEnabled = True
    End Sub

    Public Sub WeldedNew()
        MsgBox("In Welded New")
    End Sub

End Class

Open in new window


xaml
<StackPanel Grid.Row="6" Orientation="Horizontal" DataContext="{Binding Mode=OneWay, Source={StaticResource ProductionWeldingObjectDataProvider}}">
    <Button Name="OrderCompleteButton" Content="Order Complete" Command="{Binding WeldingComplete}" CommandParameter="OrderCompleteButton.tag" />
</StackPanel>

Open in new window



I don't think I'm getting the Object out of the Button Tag correctly in the xaml

Any Help would be Appreciated - (Once again a Simple thing in Winforms is made totally complex..)
Visual Basic.NET.NET Programming

Avatar of undefined
Last Comment
Ron Kidd

8/22/2022 - Mon