I'm new to the .Net world and am diving into the fun and challenging aspects of MVVM, classes, and binding with WPF.
I've managed to get my first application up and running quite nicely so far, which is a basic Time Clock. The idea is that I'm trying to use a single button, tied to an Icommand, which handles "Clock In", "Clock Out", and "Commit Record". I have everything all bound and did manage to get this to work, but I'm hoping someone can help clarify as to why the binding only works when I set the public property through the method, whereas it does not work when i set the associated private variable.
So, hopefully this will provide a more clear view of what I'm talking about:
In the ViewModel of the View in question, I have these snippets:
Private _NewClockInTime As Nullable(Of Date)
Public Property NewClockInTime As Nullable(Of Date) Get Return _NewClockInTime End Get Set(value As Nullable(Of Date)) _NewClockInTime = value NotifyPropertyChanged("NewClockInTime") End Set End Property
Public Sub ExecAddTimeRecord(ByVal param As Object) 'Check the current state Select Case Me.AddTimeRecordState Case "Clock In" 'Set New Clock In Time _NewClockInTime = Date.Now 'Change State _AddTimeRecordState = "Clock Out" Case "Clock Out" 'Set New Clock Out Time Me.NewClockOutTime = Date.Now 'Change State Me.AddTimeRecordState = "Commit Record" Case "Commit Record" Call MsgBox("Send to DB") 'Clear Values 'Change state to start "Clock In" Me.AddTimeRecordState = "Clock In" End Select End Sub
<TextBox Text="{Binding NewClockInTime, Mode=TwoWay}" Height="30"/><TextBox Text="{Binding NewClockOutTime, Mode=TwoWay}" Height="30"/>
<Button Content="{Binding AddTimeRecordState, Mode=TwoWay}" Height="30" HorizontalAlignment="Stretch" VerticalAlignment="Top" Command="{Binding AddTimeRecord, Mode=TwoWay}"/>
So, in the above code, I've actually shown my two different implementations. For Case "Clock In", I'm using the private variable tied to "NewClockInTime", for the other two, I'm using the public property. If I run the application like this, the TextBox bound to NewClockInTime never updates, this is despite the fact that I know the public property NewClockInTime is, in fact, changing through the use of the Immediate Window while stepping through the code. Also, the Content attribute for the Button never reads "Clock Out". If I switch the implementation to use the public property for all three "states", everything works as I expect.
Can anyone explain to me why, when all of these elements exist within the same ViewModel, it doesn't work when I try to set the private variables rather than the public properties? I expected that implementing INotifyPropertyChanged for these Public properties would cause the binding to work when I used the private variable within a method in the same class.
Thanks in advance for explaining this to a confused newbie! :)