1

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Of course! I had forgotten I had changed the property name. Incidentally the binding doesn't need to be set explicitly as TwoWay as I think that this is the default. Thanks!
Yes. To clarify onPropertyChanged("TemplateEditName") should be onPropertyChanged("Name") or the property must be renamed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.