AGB
AGB

Reputation: 2448

WPF Two controls bound to single property not updating each other

I am developing a WPF application using the MVVM design pattern.

I have a ViewModel which implements INotifyPropertyChanged via it's base class. The ViewModel contains a property which is then bound to two text boxes. Relevant code below.

ViewModelBase

Public MustInherit Class ViewModelBase : Implements INotifyPropertyChanged
    Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
    Protected Sub onPropertyChanged(propertyName As String)
        If Not propertyName Is Nothing Then RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
    End Sub
End Class

ViewModel

Public Class TemplateEditVM : Inherits ViewModelBase
    Public Property Name As String
      Get
        Return _Template.Name
      End Get
      Set(value As String)
        If Not _Template.Name = value Then
          _Template.Name = value
          onPropertyChanged("TemplateEditName")
        End If
      End Set
    End Property
End Class

View

<TextBox Text="{Binding Path=Name}" />
<TextBox Text="{Binding Path=Name}" />

The value of the property Name correctly populates both text boxes when the View is first loaded. The problem is that when I change the text in one of the text boxes the text of the other doesn't change, even though the underlying property has. If I step through it I can see that the PropertyChanged event is being fired.

Can anybody tell me why? Many thanks for any help.

Upvotes: 1

Views: 454

Answers (1)

JMan
JMan

Reputation: 2629

Set your binding mode to Twoway

    <TextBox Text="{Binding Path=Name, Mode="TwoWay"}" />

And your on propertyChanged method should math the exact name you bind to

Upvotes: 2

Related Questions