开发者

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 t开发者_如何学ChisPage 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


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.


Why are you using Textchanged evetn in two way binding you dont need kind of stuff. two way binding is directly bind values from your view to property and from property to view

so don't use textchanged event and try again. this will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜