Link to home
Start Free TrialLog in
Avatar of rcl58
rcl58

asked on

WPF PropertyChanged Notification from Application to Window

I have an Application that opens a Window based on some events. When the Window is loaded I bind MyClass to some controls. This binding is working fine but I’m not getting PropertyChanged notifications.

MyClass implements INotifyPropertyChanged. MyClass is updated periodically in the Application but my Window’s reference to MyClass is not seeing the change.

How can I get MyClass to refresh in the Window after my Application changes MyClass?

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        var res = (MyResource)Application.Current.FindResource("MyRes");
        var cntx = (MyContext)res.DataContext;
        MyClassProp = cntx.MyClass;
        cntx.MyClass.PropertyChanged += MyClass_PropertyChanged; // Not being triggered
    }

Open in new window

Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Could be a number of things so we'd probably need to see the code from your MyClass class - specifically the property setters and the Notify code.
Avatar of rcl58
rcl58

ASKER

I've changed my tactics a little and I'm now passing MyClass through the Window's constructor.  I don't think the property setters in MyClass is the problem because MyClass is a CSLA business object which I've never had a problem with binding/notification.

My Application is  windowless with MyResources as a merged ResourceDictionary and it self assigns a ViewModel. MyClass is a property in the ViewModel. My ViewModel opens the new window passing MyClass.

ViewModel:
 Application.Current.MainWindow = new MainWindow(MyClass);
 Application.Current.MainWindow.Show();

Open in new window

MainWindow.xaml: (added DataContext)
DataContext="{Binding RelativeSource={RelativeSource Self}}"

Open in new window

MainWindow: (Problem: _myClass_PropertyChanged handler is never called so I can refresh the window.)
public MainWindow(MyClass myClass)
   {
       InitializeComponent();
       _myClass = myClass;
       _myClass.PropertyChanged += _myClass_PropertyChanged;
       this.Loaded += new RoutedEventHandler(MainWindow_Loaded);
   }

Open in new window

To recap, my MainWindow needs to reflect changes made to MyClass that are made in my ViewModel.
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rcl58

ASKER

Ok, I did that and it binds fine but still no refresh when MyClass changes in the ViewModel. I added back the handler but it never gets called. Strange.
_myClass.PropertyChanged += _myClass_PropertyChanged;

Open in new window

Sounds to me like you maybe haven't got the INotify implemented properly in MyClass. If it was implemented correctly, you shouldn't need to explicitly bind to the PropertyChanged event to trigger changes in your UI - the WPF Binding model takes care of all that itself.
Avatar of rcl58

ASKER

Yeah I think you're right. I tried hooking up _myClass_PropertyChanged on the App ViewModel side and it's not getting called either. The problem may be with my ViewModelBase class that handles MyClass. I'm going to check with my CSLA forum guys. I'll let you know what I find out. Thanks
_myClass.PropertyChanged += _myClass_PropertyChanged;

Open in new window

Avatar of rcl58

ASKER

Yep, problem in my ViewModel. Thanks for your help!
No worries. Pleased you got it sorted :)