2010-07-01 3 views
0

MVVM을 처음 사용했습니다.MVVM의 ViewModel에서 업데이트되는 값에 바인딩 하시겠습니까?

I는 다음과 같습니다 내보기에 라벨이 있습니다

<Label Content="{Binding Path=ClockTime}" /> 

을 그리고 내 ViewModel에 보이는 같은 :

Public Class MainWindowViewModel 
    Inherits ViewModelBase 

    Dim strClockTime As String 
    Dim dstDispatcherTimer As New Windows.Threading.DispatcherTimer 

    Public Sub New() 
    AddHandler dstDispatcherTimer.Tick, AddressOf TimeDelegate 
    dstDispatcherTimer.Interval = New TimeSpan(0, 0, 1) 
    dstDispatcherTimer.Start() 
    End Sub 

    Private Sub TimeDelegate(ByVal sender As Object, ByVal e As System.EventArgs) 
    strClockTime = DateTime.Now.ToString("dddd, dd MMMM yyyy h:mm:ss tt") 
    End Sub 

    Public ReadOnly Property ClockTime As String 
    Get 
     Return strClockTime 
    End Get 
    End Property 

End Class 

내 문제 레이블이 스레드와 동적으로 업데이트하지 않는다는 것입니다 ViewModel에서. 보기가이 값이 동적임을 쉽게 알 수있는 간단한 방법이 있습니까? 당신은 토드이 당신이 뭔가를보고 ViewModelBase이 필요한 것처럼 당신이 당신의 코드를 업데이트해야합니다 말에 확장하려면 클럭 타임

답변

4

당신은 당신의 ViewModel의에서 INotifyPropertyChanged 인터페이스를 구현하고하여 PropertyChanged 이벤트를 발생합니다

Public class ViewModelBase Inherits INotifyPropertyChanged 
    protected Sub OnPropertyChanged(PropertyName as string) 
    if PropertyChanged is not nothing then 
     PropertyChanged(new PropertyChangedEventArgs(PropertyName) 
    end if 
    End Sub 
End Class 

한 다음과 같이 뷰 모델을 수정

Private Sub TimeDelegate(ByVal sender As Object, ByVal e As System.EventArgs) 
    ClockTime = DateTime.Now.ToString("dddd, dd MMMM yyyy h:mm:ss tt") 
    End Sub 

    Public Property ClockTime As String 
    Get 
     Return strClockTime 
    End Get 
    Private Set 
     strClockTime = value 
     OnPropertyChanged("ClockTime") 
    End Set 
    End Property 

Notic 내가 ClockTime에 할당하고있어 WPF에 ClockTime이 변경되었다는 알림이 표시되면

관련 문제