0

I have a situation where my view has a DataContext bound to the ViewModel but one of my controls inside the view has its DataContext Set to a property of the ViewModel. The first time I change that ViewModel, it shows the changes but after that, if I change property inside the ViewModel no changes are reflected back to the view.

//Somewhere inside my View <TaicoControl:FlashMessage DataContext="{Binding FlashMessage}" DockPanel.Dock="Top" FadesOutAutomatically="True" FontFamily="BPG Arial" Message="{Binding Message}" MessageType="{Binding FlashType}" /> //End of the View public sealed class ShellViewModel : ViewModelBase { public FlashMessageModel FlashMessage { get; private set; } protected override void SetupEvents() { RegisterForEvent<SystemBaloonRequiered>(OnBaloonRequest); RegisterForEvent<FlashRequest>(OnFlashRequested); base.SetupEvents(); } #region Message Handlers private void OnFlashRequested(FlashRequest obj) { FlashMessage = null; FlashMessage = new FlashMessageModel { Message = obj.Message, FlashType = obj.FlashType }; RaisePropertyChanged(() => FlashMessage); } } 
4
  • 2
    Can you paste the binding expression and the view model property being utilized? Commented Oct 18, 2012 at 17:01
  • 4
    Make sure the Mode of the binding is not OneTime and the ViewModel and the property implement INotifyPropertyChanged correctly. Post your code to get feedback Commented Oct 18, 2012 at 17:07
  • Double checked it is set up correctly I even posted code here Commented Oct 18, 2012 at 17:28
  • Are you sure it isn't updating the DataContext? Use a tool like Snoop WPF Spy to spy on what the DataContext of the user control is. I'll bet that it is actually the new object and the error lies within that control. Also, ensure that you are calling your OnFlashRequested using breakpoints. Commented Oct 18, 2012 at 18:08

1 Answer 1

1

Explanation:

This is the classic case of not implementing INotifyPropertyChanged interface.

When you change the value of FlashMessage there is no way for the UI to know that. So, to let the UI know, you raise PropertyChanged event with the property name ("FlashMessage" in your case).

Once you implement INotifyPropertyChanged interface and notify property change for FlashMessage it should workout just fine.

Example:

public sealed class ShellViewModel : ViewModelBase, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string propertyName) { var temp = PropertyChanged; if(temp != null) { temp(this, new PropertyChangedEventArgs(propertyName)); } } public FlashMessageModel _flashMessage; public FlashMessageModel FlashMessage { get { return _flashMessage; } private set { _flashMessage = value; RaisePropertyChanged("FlashMessage"); } } } 

-EDIT-

Try changing your code from following:

FlashMessage = null; FlashMessage = new FlashMessageModel { Message = obj.Message, FlashType = obj.FlashType }; RaisePropertyChanged(() => FlashMessage); 

to following:

FlashMessage.Message = obj.Message; FlashMessage.FlashType = obj.FlashType; 
Sign up to request clarification or add additional context in comments.

9 Comments

Where Did u see INotifyPropertyChanged not being implemented? Viewmodelbase already implements that so there is no need to implement this in concrete viewmodel. please ask before making notes
In general, ViewModelBase should be implementing INotifyPropertyChanged. Otherwise this is the correct approach.
Id Does implement and I use that Base type on every ViewModel classes and there are no problems code.msdn.microsoft.com/windowsdesktop/… this is the control I am trying to use and after it closes I am not able to show it again using above code
Have you implemented property notifications for Message and FlashType? If yes, what if you don't create a new object of FlashMessageModel and just assign new values to Message and FlashType properties? I think there is a bug that when DataContext is set to null once, it stops updating the bindings.
U mean to directly expose message and type properties from ShellViewModel?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.