0
votes

G'day everyone,

I have searched and searched for an answer to this, would really appreciate someone's help! I am sure this is something simple that I'm missing...

My DataGrid appears to Bind correctly, and updates correctly, however the Textboxes and Checkboxes don't update. They are initially set correctly, but wont update when another DataGrid item is selected.

If I set the Textbox Binding directly to the Selected object String, e.g. <TextBox x:Name="txtLastName" Text="{Binding Selection.LastName, Mode=TwoWay}" then the Textboxs do update on new selection (and work correctly). But Binding the Checkboxes in the same way doesn't work. I also believe this is not best practice...? Isn't it better to Bind to the ViewModel LastName() property?

I have a MembersView with a DataGrid and several Textboxes and Checkboxes:

<DataGrid ItemsSource="{Binding Path=MemberCollection}" SelectedItem="{Binding Path=Selection, Mode=TwoWay}" Name="DataGrid1"/>

<TextBox x:Name="txtFirstName" Text="{Binding FirstName, Mode=TwoWay}" />
<TextBox x:Name="txtLastName" Text="{Binding LastName, Mode=TwoWay}" />

<CheckBox x:Name="chbChairman" IsChecked="{Binding GeneralChairman}" Content="Chairman" />
<CheckBox x:Name="chbFillins" IsChecked="{Binding GeneralFillins}" Content="Fillins" />

Codebehind sets: DataContext = memberViewModel

MembersViewModel contains:

Public Class MemberViewModel
Inherits ViewModelBase  ' This implements INotifyPropertyChanged

Private _objMember As Member
Private _MemberCollection As MemberCollection
Dim _selectedMember As Member

Public Property Selection() As Member
    Get
        Return _selectedMember
    End Get
    Set(ByVal value As Member)
        If value Is _selectedMember Then
            Return
        End If
        _selectedMember = value
        OnPropertyChanged("Selection")
    End Set
End Property

Public Property MemberCollection As MemberCollection
    Get
        Return _MemberCollection
    End Get
    Set(ByVal value As MemberCollection)
        _MemberCollection = value
        OnPropertyChanged("MemberCollection")
    End Set
End Property

Public Property Member() As Member
    Get
        Return _objMember
    End Get
    Set(ByVal Value As Member)
        _objMember = Value
        OnPropertyChanged("Member")
    End Set
End Property

Public Property FirstName() As String
    Get
        Return _objMember.FirstName
    End Get
    Set(ByVal Value As String)
        _objMember.FirstName = Value
        OnPropertyChanged("FirstName")
    End Set
End Property

Public Property LastName() As String
    Get
        Return _objMember.LastName
    End Get
    Set(ByVal Value As String)
        _objMember.LastName = Value
        OnPropertyChanged("LastName")
    End Set
End Property

Public Property GeneralChairman() As Boolean
    Get
        Return _objMember.MemberUseFor.GeneralChairman
    End Get
    Set
        _objMember.MemberUseFor.GeneralChairman = Value
        OnPropertyChanged("GeneralChairman")
    End Set
End Property

Public Property GeneralFillins() As Boolean
    Get
        Return _objMember.MemberUseFor.GeneralFillins
    End Get
    Set
        _objMember.MemberUseFor.GeneralFillins = Value
        OnPropertyChanged("GeneralFillins")
    End Set
End Property

My Member model contains:

Public Property FirstName As String
Public Property LastName As String
Public Property MemberUseFor As MemberUseFor

and MemberUseFor contains:

Public Property GeneralChairman As Boolean
Public Property GeneralFillins As Boolean

The Member and MemberUseFor classes don't implement INotifyPropertyChanged, but I think that is correct...?

Would greatly appreciate someone's help with why the selecting differet rows in the DataGrid won't update my Text Boxes and CheckBoxes.

Thanks again! Damian

1

1 Answers

0
votes

You are confusing a few things here:

  1. Your Member property doesn't seem to have any use. Selection and Member appear to be doing the same job. So remove Member property and its underlying local member _objMember.
  2. Selection property would better be named as SelectedMember if you want to follow Microsoft conventions.
  3. FirstName and LastName should really be defined inside Member class, not in the MembersViewModel.
  4. Binding of your TextBox and CheckBox objects would then be in the form Text="{Binding SelectedMember.LastName, Mode=TwoWay}" etc.
  5. MemberViewModel as well as Member class should implement INotifyPropertyChanged. Even better would be to use some MVVM framework such as MVVM Light. This will make your job FAR easier.