当选择DataGrid项时,VB.net WPF Databinding不更新TextBox

时间:2016-01-17 09:22:08

标签: asp.net wpf vb.net datagrid

G'day大家,

我搜索并搜索了这个答案,非常感谢别人的帮助!我确信这很简单,我很想念......

我的DataGrid似乎绑定正确,并且正确更新,但文本框和复选框不会更新。它们最初设置正确,但在选择另一个DataGrid项时不会更新。

如果我将Textbox Binding直接设置为Selected对象String,例如<TextBox x:Name="txtLastName" Text="{Binding Selection.LastName, Mode=TwoWay}"然后文本框执行更新新选择(并正常工作)。但以相同的方式绑定复选框不起作用。我也相信这不是最佳做法......?绑定到ViewModel LastName()属性不是更好吗?

我有一个带有DataGrid的 MembersView 以及多个文本框和复选框:

<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" />

代码隐藏集:DataContext = memberViewModel

MembersViewModel 包含:

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

我的会员模型包含:

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

MemberUseFor 包含:

Public Property GeneralChairman As Boolean
Public Property GeneralFillins As Boolean

会员 MemberUseFor 类没有实现INotifyPropertyChanged,但我认为这是正确的......?

非常感谢有人帮助解决为什么在DataGrid中选择差异行不会更新我的文本框和复选框。

再次感谢! 达米安

1 个答案:

答案 0 :(得分:0)

你在这里混淆了一些事情:

  1. 您的Member财产似乎没有任何用处。 SelectionMember似乎正在做同样的工作。因此,请删除Member属性及其基础本地成员_objMember
  2. 如果您想遵循Microsoft惯例,
  3. Selection属性最好命名为SelectedMember
  4. FirstNameLastName应该在Member类中定义,而不是MembersViewModel
  5. TextBoxCheckBox对象的绑定将采用Text="{Binding SelectedMember.LastName, Mode=TwoWay}"等格式。
  6. MemberViewModel以及Member类应该实现INotifyPropertyChanged。更好的是使用一些MVVM框架,例如MVVM Light。这将使您的工作变得更加容易。