2014-01-08 5 views
0

저는 MainWindow와 사용자 정의 컨트롤 인 DataDetails가 있습니다. 두 뷰 모두 viewmodels입니다.Wpf 사용자 컨트롤이 ui에서 변경된 값을 표시하지 않음

DataDisplay 사용자 컨트롤을 사용하여 현재 선택된 항목을 MainWindow 목록 상자에 표시합니다. 장면 뒤에 모든 것이 예상대로 작동하지만 DataDetailsViewModel 내부의 디버깅 데이터가 올바르게 변경되었지만 UI가이 변경된 데이터를 표시하도록 할 수는 없습니다.

여기

DataDetailsViewModel.cs 몇 가지 코드를입니다

public string Title { get; set; } 
public string Edition {get; set;} 

private void SetSelectedBook_Mediator(object args) 
{ 
    Book b = (Book)args; 
    SelectedBook = b; 
    SetData(); 
} 

private void SetData() 
{ 
    // on debugging Title and Edition are properly populated 
    Title = SelectedBook.Title; 
    Edition = SelectedBook.Edition; 
} 

DataDetails.xaml

<TextBox Name="txtTitle" Text="{Binding Title}" /> 
<TextBox Name="txtEdition" Text="{Binding Edition}" /> 

답변

3

귀하의 viewmodels는 INotifyPropertyChanged 인터페이스를 구현해야합니다.

이 인터페이스에는 WPF에서 데이터 바인딩을 설정할 때 UI가 자동으로 구독하는 이벤트가 포함됩니다. 속성이 변경되면 관심이있는 사용자 (사용자 UI)에게 업데이트해야한다고 알리기 위해 이벤트를 발생시켜야합니다.

자세한 내용은 the MSDN page을 참조하십시오.

public class DataDetailsViewModel : INotifyPropertyChanged { 
    private string _title; 
    public string Title { 
     get { return _title; } 
     set { _title = value; NotifyPropertyChanged("Title"); } 
    } 

    // Other properties, methods, etc... 

    public PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged(string name) { 
     var handlers = PropertyChanged; 
     if (handlers != null) 
      handlers(this, new PropertyChangedEventArgs(name)); 
    } 
} 
관련 문제