none
Binding in code from textbox to collection RRS feed

  • Pregunta

  • Hi everybody  :D

    I try to bind a textbox to a collection, so if the value of the item in the collection changes the textbox.text changes… but I cannot get it to work.

    When I set the binding it gets the correct value, but if the collection changes the value don’t. I don’t know how to update it. I tried the examples in the help, but they don’t work for me.

        Public Class Valor
            Implements INotifyPropertyChanged
    
            ' Events
            Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
            ' Methods
            Public Sub New(ByVal vvv As Integer)
                Me._vvv = vvv
            End Sub
    
            ' Properties
            Public Property vvv() As Integer
                Get
                    Return Me._vvv
                End Get
                Set(ByVal value As Integer)
                    Me._vvv = value
                    Me.OnPropertyChanged()
                End Set
            End Property
    
            Private Sub OnPropertyChanged()
                RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("vvv"))
            End Sub
    
            ' Fields
            Private _vvv As String
        End Class
    
    
        Public MyCantidades As New ObservableCollection(Of Valor)
    
                    'Cantidad
                    Dim Cantidad1 = New TextBlock
                    Cantidad1.FontSize = 30
                    Dim Enlace As Binding = New Binding()
                    Enlace.Source = MyCantidades.Item(3).vvv
                    Enlace.Mode = 1
                    Cantidad1.SetBinding(TextBlock.TextProperty, Enlace)
    
                    ListaVarios.Children.Add(Cantidad1)

    and one boton to change the collection

        Private Sub MasClick(sender As Object, e As RoutedEventArgs)
    
            MyCantidades.Item(3).vvv = MyCantidades.Item(3).vvv + 1
    
        End Sub

    Ok, when I press the button the collection changes, but the textblock doesnt update.

    ¿Any clues?

    Thank you in advance :D


    sábado, 16 de febrero de 2013 13:08

Respuestas

  • Ok MarkT gave me the answer :D

    You are binding to an Integer "vvv" which is not a Valor and which does not raise the PropertyChanged event.

    Bind to the Valor object and set the Path to access vvv.

    Enlace.Source = MyCantidades.Item(3)
    Enlace.Path = New PropertyPath("vvv")

    • Marcado como respuesta dlivian sábado, 16 de febrero de 2013 18:29
    sábado, 16 de febrero de 2013 18:29