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

rcl58Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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.
0
rcl58Author Commented:
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.
0
Chris StanyonWebDevCommented:
OK. If you're passing the myClass into the ctor of the window and you want your window to bind to that, then in the ctor, set the DataContext:

public MainWindow(MyClass myClass)
{
    InitializeComponent();
    DataContext = myClass;
}

Open in new window

Your window now has it's DataContext set to the myClass instance, and assuming your myClass implements the INotify, you just bind directly to the properties of myClass:

<TextBlock Text="{Binding someProperty}"/>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

rcl58Author Commented:
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

0
Chris StanyonWebDevCommented:
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.
0
rcl58Author Commented:
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

0
rcl58Author Commented:
Yep, problem in my ViewModel. Thanks for your help!
0
Chris StanyonWebDevCommented:
No worries. Pleased you got it sorted :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WPF

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.