Silverlight 5 INotify OnPropertyChanged未触发?

时间:2012-11-07 23:21:33

标签: silverlight binding

如果我将TextBox绑定到对象(Merchandise是对象)属性,如:

Text =“{Binding Path = Merchandise.Quantity,Mode = TwoWay}

<TextBox x:Name="QuantityTextBox" Grid.Column="1" Grid.Row="5" Width="70" Text="{Binding Path=Merchandise.Quantity, Mode=TwoWay, TargetNullValue='0', FallbackValue='0', ValidatesOnExceptions=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/>

DataContext设置为我的ViewModel,它包含(以及其他属性)Merchandise类型的声明属性。

    Implements INotifyPropertyChanged, IDataErrorInfo

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

Public Sub OnPropertyChanged(ByVal e As PropertyChangedEventArgs)

    If PropertyChangedEvent IsNot Nothing Then

        RaiseEvent PropertyChanged(Me, e)

        Select Case e.PropertyName


            Case "Merchandise"

                DoSomething()

            Case Else

        End Select

    End If

End Sub


 Private _Merchandise As DC.SL.Services.WebServiceMerchandise.Merchandise
Public Property Merchandise() As DC.SL.Services.WebServiceMerchandise.Merchandise
    Get
        Return _Merchandise
    End Get
    Set(ByVal value As DC.SL.Services.WebServiceMerchandise.Merchandise)
        _Merchandise = value
        OnPropertyChanged(New PropertyChangedEventArgs("Merchandise"))
    End Set
End Property

当我在TextBox中输入更改时,不会触发PropertyChanged事件。我可以解决这个问题的唯一方法是在我的ViewModel中创建重复的属性...即Quantity,然后在单击Ok按钮时我将ViewModel属性分配给我的Merchandise对象。但这绝对没有效率似乎打败了能够使用Path = SomeObject.Property绑定的目的。

有关如何解决此问题的任何提示?

谢谢,Rob。

1 个答案:

答案 0 :(得分:0)

您需要针对PropertyChanged课程的属性举办Merchandise个活动。因此该类需要实现INotifyPropertyChanged接口以及视图模型类。否则,用户界面不知道有任何改变。