我对 MVVM 和 MVVM light 非常陌生,而且我来自 vb.net 背景。我设法创建了一些简单的学习项目,在一个视图中,按钮没有调用命令。我的视图 xaml 如下(为便于阅读而缩短):
<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>
以下是按钮功能的viewmodel类中的代码
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
因此,此代码旨在根据提供的值更改父数组以包含除提供的值之外的所有项目。然后它将数组值分配给页面上的按钮。一切都在按钮内容绑定方面起作用,但由于某种原因,命令没有触发。我已经在另一个视图模型上使用了这种精确的方法并且它有效,
我需要一些帮助才能知道为什么这不适用于此视图模型
提前致谢
约旦国王