Search code examples
wpfvb.netbindinginotifypropertychangedtwo-way

wpf two way binding not working


i have

 <Grid Name="thisPage">
  <TextBlock Name="tbtb" />
  <ScrollViewer Name="sv4" Visibility="Hidden">
    <ItemsControl ItemsSource="{Binding}">
                 <ItemsControl.ItemTemplate>
                     <DataTemplate>
                       <TextBox Text="{Binding Title, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextChanged="TextBox_TextChanged"/> 
                     </DataTemplate> 
                 </ItemsControl.ItemTemplate> 
             </ItemsControl>
   </ScrollViewer>
  </Grid>

in the MainWindow.vb, i have

movieArray as ObservableCollection(of Movie)

For i As Integer = 0 To 5 
        Me.movieArray.Add(New Movie(i)) 
    Next

Me.sv4.DataContext = Me.movieArray
Me.listBox5.DataContext = Me.movieArray

 Private Sub TextBox_TextChanged(sender As System.Object, e As System.Windows.Controls.TextChangedEventArgs)

        Me.tbtb.Text = ""
        For Each m As Movie In movieArray
            Me.tbtb.Text += p.Title.ToString + " ^ "
        Next
       End Sub

Class Movie
    Implements INotifyPropertyChanged

  Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

 Property Title As Integer
        Get
            Return Me._title
        End Get
        Set(value As Integer)
            Me._title = value
            If Not (value = _title) Then
                Me._title= value
                NotifyPropertyChanged("Title")
            End If
        End Set
    End Property 

for the next page i have,

 <Grid Name="nextPage" Visibility="Hidden" > 
            <ListBox Name="listBox5" >
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Title}"/>
                    </DataTemplate> 
                </ItemsControl.ItemTemplate> 
            </ItemsControl>
        </ListBox>
        </Grid > 

To change pages i just toggle the visibility of thisPage and nextPage using back, next buttons.

IM not sure what im doing wrong as:-

  1. listbox5 shows only the original values, not anything changed by textboxes.
  2. tbtb, however is able to update its values

Solution

  • I think the problem might be your 'Title' property setter.

    I'm a C# guy, not a VB expert... but it would appear that NotifyPropertyChanged will never get called.

    value = _title will always be true because you just set Me._title = value in the previous line of code. Thus you will never execute any of the code in your if statement.