-2

I have a rather simple case but I can't get the correct solution to it. On my MVVM application, I would like my MainView (attached to MainViewModel) to display current user logon (name) through binding.

User class is a POCO implementing INPC. The CurrentUser instance is a static property of my application controller AppController. The LogOn
property is the string I want to display in MainView.

So short version : I want to bind AppController.CurrentUser.LogOn in MainView.

I tried several ways of static binding (like in this article ), I can't get it to work.

Any idea of out to make this work ?

Thanks.

EDIT:

The problem is that initial binding works, but when I set another User object to current user , the LogOn property is not refreshed, even though AppControl.CurrentUser property raises a static event PropertyChanged.

EDIT 2

I tried @Henk Holterman solution (echo property from ViewModel) ,and debugging it I found the trick :In fact , the problem was a race condition : my ViewModel was raising the UserChanged event through an EventAggregator , and then raises PropertyChanged , but it was too quick and object reference was not changed on AppController when event raises.

Thank you very much all for your help !

o.schwab
  • 101
  • 1
  • 7
  • 1
    Does the LogOn property notify that is has been changed, (i.e. calls OnNotifyPropertyChanged in its setter)? – Alex Oct 26 '17 at 07:05
  • 1
    More importantly - does the `AppController.CurrentUser` (potentially) change while `MainView` is loaded? – Grx70 Oct 26 '17 at 07:07
  • 1
    `{Binding LogOn, Source={x:Static local:AppController.CurrentUser}}` should work. – Clemens Oct 26 '17 at 07:50
  • an you should be sure, that AppController.CurrentUser is set before the View is loaded! Because CurrentUser is a static member, it cannot raise PropertyChanged event. – lvoros Oct 26 '17 at 07:53
  • 1
    You could also echo the property as `MainViewModel.CurrentUser`, no need for the View to know what your deeper architecture is. – H H Oct 26 '17 at 07:54
  • @Alex yes, LogOn implements by inheriting BaseINPC [https://pastebin.com/ratEvS6z] (BaseINPC) [https://pastebin.com/VgESGyCP] (User class) – o.schwab Oct 26 '17 at 07:55
  • OK I missed a part of my explanation ; editing the description – o.schwab Oct 26 '17 at 07:59
  • @Ivoros There is `StaticPropertyChanged` since WPF 4.5. – Clemens Oct 26 '17 at 08:16

1 Answers1

1

Your AppController class may look like this:

public class User
{
    public string LogOn { get; set; }
}

public static class AppController
{
    public static event PropertyChangedEventHandler StaticPropertyChanged;

    private static User currentUser;
    public static User CurrentUser
    {
        get { return currentUser; }
        set
        {
            currentUser = value;
            StaticPropertyChanged?.Invoke(null,
                new PropertyChangedEventArgs(nameof(CurrentUser)));
        }
    }
}

Then the Binding might be written like this:

<TextBlock Text="{Binding Path=(local:AppController.CurrentUser).LogOn}"/>
Clemens
  • 123,504
  • 12
  • 155
  • 268