J King J King - 1 year ago 86
Vb.net Question

MVVM Light in vb.net Button with command parameter

I am very new to MVVM and MVVM light and I come from a vb.net background. I have managed to create some simple projects for learning and in one view the buttons are not calling the commands. I have the view xaml as follows(shortened for readability):

<UserControl x:Class="FTC.View.NavigationView"
DataContext="{Binding NavigationView, Source={StaticResource Locator}}" >

<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" />

<Button HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top"
Command="{Binding SwapParent}"
CommandParameter="{Binding Parent1}" Background="{x:Null}" BorderBrush="{x:Null}" Width="130" Height="45">
<TextBlock Text="{Binding Parent1}" Foreground="#FFE4E4E4"/>

And the following is the code in the viewmodel class for the button function

Private _sNavs() As String = {"Dashboard", "Jobs", "People", "Products", "Financials", "Admin"}
Private _sParentNavs() As String
Public Property SwapParent As RelayCommand
Private Property ParentNavs() As String()
Return _sParentNavs
End Get

Set(value As String())
If _CurrentParent = String.Empty Then
_CurrentParent = "Dashboard"
If _sParentNavs.Contains(_CurrentParent) = False Then
End If
End If


End Set
End Property

Public Sub New()

If _CurrentParent = String.Empty Then
_CurrentParent = "Dashboard"
End If
SwapParent = New RelayCommand(Sub()
ParentNavs = _sParentNavs
End Sub)

End Sub

Private Sub RefreshParents(sCurrent)

If _sNavs.Contains(sCurrent) = True Then
_sParentNavs = Filter(_sNavs, sCurrent, False, CompareMethod.Text)
_sParentNavs = Filter(_sNavs, "Dashboard", False, CompareMethod.Text)
End If

Parent1 = _sParentNavs(0)
Parent2 = _sParentNavs(1)
Parent3 = _sParentNavs(2)
Parent4 = _sParentNavs(3)
Parent5 = _sParentNavs(4)

End Sub

so this code is meant to change the parent array based on the supplied value to include all items except the supplied value. Then it assigns array values to buttons on the page. Everything works in terms of the button content binding, but for some reason the command is not firing. I have used this exact method on another view model and it works,

I need some help to know why this is not working on this viewmodel

thanks in advance

jordan king

Answer Source

I changed they way I defined my command objects.

I re-worked my project and no longer use the above code but he following is some examples of using lambdas in vb to define command objects where the addressof refers to public Sub/Functions in the class.

"Of String" receives a command parameter of type string to the function named by the addressof. The second address of is a function that returns a boolean, which gets evaluated for the command's canexecute method.

NavigateShellCommand = New RelayCommand(Of String)(AddressOf NavigateShell)
NavigateChildCommand = New RelayCommand(Of String)(AddressOf NavigateChild)
IsParentEnabled = New RelayCommand(Of String)(AddressOf NavigateChild, AddressOf ParentEnabled)
IsChildEnabled = New RelayCommand(Of String)(AddressOf NavigateChild, AddressOf ChildEnabled)

i hope this can clear help someone, it took me a while to really understand it

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download