目标
我希望在btnRefresh
文本框中有内容时启用按钮tbMachineNo
(使用 MVVM 标准)。
项目总结
我有一个:
窗口.xaml
<TextBox Text="{Binding Inspection.Machine.MachineNumber, UpdateSourceTrigger=PropertyChanged}" />
<Button Command="{Binding RefreshMachineNo}" />
检查视图模型.vb
这有一个属性InspectionModel
并包含几个方法。一个是我的 ICommand 在我的构造函数期间被执行(这就是由于该CanUpdate
方法而禁用/启用我的文本框的原因)
Public Class InspectionViewModel
Private _Inspection As InspectionModel
Private _RefreshMachineNo As ICommand
Public Property Inspection As InspectionModel
Get
Return _Inspection
End Get
Set(value As InspectionModel)
_Inspection = value
End Set
End Property
Public Sub New()
_Inspection = New InspectionModel("Version", "Title of machine", "Model", "Owner", "Department", Date.Now, New MachineModel)
RefreshMachineNo = New RefreshMachineNumberCommand(Me) 'Calls the CanUpdate, and if it returns true, it executes FetchMachineDetails()
End Sub
Public Property RefreshMachineNo As ICommand
Get
Return _RefreshMachineNo
End Get
Set(value As ICommand)
_RefreshMachineNo = value
End Set
End Property
Public ReadOnly Property CanUpdate As Boolean
Get
If Inspection Is Nothing Then
Return False
End If
Return Not String.IsNullOrWhiteSpace(Inspection.Machine.MachineNumber)
End Get
End Property
Public Sub FetchMachineDetails()
Dim MachineNo As String = Inspection.Machine.MachineNumber
End Sub
End Class
这工作正常,代码在它应该执行的时候执行。现在看看我的 InpsectionModel。
RefreshMachineNumber 命令
Public Class RefreshMachineNumberCommand
Implements ICommand
Private _viewModel As InspectionViewModel
Public Sub New(ByVal viewModel As InspectionViewModel)
_viewModel = viewModel
End Sub
Public Event CanExecuteChanged(sender As Object, e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged
Public Function CanExecute(parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
Return _viewModel.CanUpdate()
End Function
Public Sub Execute(parameter As Object) Implements System.Windows.Input.ICommand.Execute
_viewModel.FetchMachineDetails()
End Sub
End Class
检查模型.vb
这个类继承了我的 ObservableObject ,它实现了INotifyPropertyChanged
. 所以理论上,每当我MachineNumber
的检查对象中的属性发生变化时Machine
,它都应该触发UpdateSourceTrigger=PropertyChanged
Public Class InspectionModel
Inherits ObservableObject
Private _Version As String
Private _Title As String
Private _Model As String
Private _InspectionOwner As String
Private _Department As String
Private _DateEntry As Date
Private _Machine As MachineModel
Public Property Version As String
Get
Return _Version
End Get
Set(value As String)
_Version = value
Notify("Version")
End Set
End Property
Public Property Title As String
Get
Return _Title
End Get
Set(value As String)
_Title = value
Notify("Title")
End Set
End Property
Public Property Model As String
Get
Return _Model
End Get
Set(value As String)
_Model = value
Notify("Model")
End Set
End Property
Public Property InspectionOwner As String
Get
Return _InspectionOwner
End Get
Set(value As String)
_InspectionOwner = value
Notify("InspectionOwner")
End Set
End Property
Public Property Department As String
Get
Return _Department
End Get
Set(value As String)
_Department = value
Notify("Department")
End Set
End Property
Public Property DateEntry As Date
Get
Return _DateEntry
End Get
Set(value As Date)
_DateEntry = value
Notify("DateEntry")
End Set
End Property
Public Property Machine As MachineModel
Get
Return _Machine
End Get
Set(value As MachineModel)
_Machine = value
Notify("Machine")
End Set
End Property
Public Sub New(ByVal Version As String, ByVal Title As String, ByVal Model As String, ByVal InspectionOwner As String, ByVal Department As String, ByVal DateEntry As Date, ByVal Machine As MachineModel)
Me.Version = Version
Me.Title = Title
Me.Model = Model
Me.InspectionOwner = InspectionOwner
Me.Department = Department
Me.DateEntry = DateEntry
Me.Machine = Machine
End Sub
End Class
问题
当文本框填充字符或为空时,我没有掌握如何启用/禁用我的按钮控件。我的CanUpdate
ICommand 是当前控制它的,并且仅在加载构造函数时执行一次。
这是有道理的,但我不确定如何在不放置代码的情况下使这个 Textchanged 问题正常工作。我希望能够使用 MVVM 来做到这一点
我将我的文本框绑定到另一个字段,并且该字段绑定到 Inspection 对象中的相同属性。当我在文本框中输入时,我可以看到另一个字段被修改......所以 UpdateSourceTrigger 正在工作,但是当我输入文本时我无法启用按钮。