0

I have MainWindow and user control DataDetails. Both views has it's viewmodels.

I use DataDisplay user control to display currently selected item in MainWindow listbox. Behind the scene everything works as expected, in debugging data inside DataDetailsViewModel is properly changed but I cannot force ui to represent this changed data.

Here's some code

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}" />

1 Answer 1

3

Your viewmodels need to implement the INotifyPropertyChanged interface.

This interface contains an event that the UI will automatically subscribe to when WPF sets up the data binding. When the property changes, the event should be raised to inform anyone interested (your UI) that it needs to be updated.

See the MSDN page for details.

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));
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.