J King J King - 1 month ago 14
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"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:FTC_Application.FTC.ViewModel"
mc:Ignorable="d"
d:DesignWidth="900"
Height="161"
DataContext="{Binding NavigationView, Source={StaticResource Locator}}" >

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

<Grid>
<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"/>
</Button>


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()
Get
Return _sParentNavs
End Get

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

RefreshParents(_CurrentParent)
RaisePropertyChanged(ParentNavsPropertyName)

End Set
End Property

Public Sub New()

If _CurrentParent = String.Empty Then
_CurrentParent = "Dashboard"
End If
RefreshParents(_CurrentParent)
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)
Else
_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

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