Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble using the TextBox UpdateSourceTrigger And PropertyChanged
    text
    copied!<h2>Goal</h2> <p>I want the button <code>btnRefresh</code> to be enabled when the Textbox <code>tbMachineNo</code> has something in it (using the MVVM standards).</p> <hr> <h2>Project Summary</h2> <p>Bare with me on this one, I'll try to explain it with detail. I have a:</p> <p><strong>Window.xaml</strong></p> <pre><code>&lt;TextBox Text="{Binding Inspection.Machine.MachineNumber, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;Button Command="{Binding RefreshMachineNo}" /&gt; </code></pre> <p><strong>InspectionViewModel.vb</strong></p> <p>This has a property of <code>InspectionModel</code> and contains several methods. One being that my ICommand gets executed during my constructor (and that's what disables / enables my textbox because of the <code>CanUpdate</code> method)</p> <pre><code>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 </code></pre> <p>This works fine, the code is executed when it should. Now take a look at my InpsectionModel.</p> <p><strong>RefreshMachineNumberCommand</strong></p> <pre><code>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 </code></pre> <p><strong>InspectionModel.vb</strong></p> <p>This class inherits my ObservableObject which is what implements the <code>INotifyPropertyChanged</code>. So in theory, whenever my property <code>MachineNumber</code> changes in the Inspection object <code>Machine</code>, it should trigger the <code>UpdateSourceTrigger=PropertyChanged</code></p> <pre><code>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 </code></pre> <hr> <h2>Problem</h2> <p>I am not grasping how to enable / disable my button control when the textbox becomes populated with characters or when it is empty. The <code>CanUpdate</code> of my ICommand is what currently controls this and only gets executed once when the constructor is loaded. </p> <p>This makes sense, but I am not sure how to make this Textchanged issue work without putting code behind. I want to be able to do it using MVVM</p> <p><strong>Edit</strong></p> <p>I bound my Textbox to another field and that field was bounded to the same attribute in the Inspection object. I could see the other field getting modified as I typed in the textbox ... So the UpdateSourceTrigger is working but I can't get the button to enable when I type text.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload