views:

273

answers:

2

I have a WPF application.

In my App.xaml.cs I save the full name of the logged in user as follows:

App.Current.Properties["FullName"] = GetValueFromDatabase();

I have two screens/windows in the same application.

First is the users scrren, which has a an editable WPF data grid where I can update full name of the current user. Second is a screen which has a Label/TextBox control which displays the Current users fullname from the Application object.

When both the windows are open, if I change the full name of the user and save the changes in the first screen. I want to trigger an event so that, it is also reflected in the second screen immediately?

Which event do I hook to this application and how?

I am using MVVM pattern and entity framework.

A: 

You should use a dependency property or make sure your model is an INotifyPropertyChanged so that others can bind to it in either the screen or window using a {Binding}

// if your model is derived from a sub-class of DependencyObject....
class Model : DependencyObject 
{
    public static readonly DependencyProperty MyStringProperty = 
      DependencyProperty.Register("MyString", typeof(string), 
      typeof(IncidentGraphic), new UIPropertyMetadata(string.Empty));

    public string MyString
    {
     get { return (string)GetValue(MyStringProperty ); }
     set { SetValue(MyStringProperty , value); }
    }
}

/// recommended to use INotifyPropertyChanged
class Model : INotifyPropertyChanged
{
     private void NotifyChange(string property)
     {
       PropertyChangedEventHandler handler = this.PropertyChanged;
       if (handler != null)
       {
         handler(this, new PropertyChangedEventArgs(property));
       }
     }

     string m_MyString = string.Empty;
     public string MyString
     {
       get
       {
         return m_MyString ;
       }
       set
       {
         if ( value != this.m_MyString )
         {
           this. m_MyString  = value;
           NotifyChange("MyString");
         }
             }
     }
}

// in your xaml.....
    <TextBox Text="{Binding MyString}" />
Muad'Dib
Thank you Chris. But considering given scenario, If I raise the event in the 1st screen, would it be immediately updated in the second? Would have to try it out.
Wpf Newbie
yes, it will pretty much update immediately. the wpf framework sends a "property changed" event and whatever is bound to the property will update itself without any intervention from you. no extra code needed.
Muad'Dib
A: 

I have implemented a Mediator class, which acts as a medium to communicate between the ViewModels of both the Windows.

I referred this article : http://sachabarber.net/?p=477

Wpf Newbie