Home > Enterprise >  How to update a TextBlock in ShellView when property changes in Model
How to update a TextBlock in ShellView when property changes in Model

Time:10-08

I am having trouble updating a ShellView TextBlock with the FirstName property of the LoggedInUserProfile which is created as a Singleton after the user has logged in.

I have a UserProfileView and this binds and updates OK, but the ShellView (which contains the UserProfileView) does not. If I put breakpoints in I can see the LoggedInUserProfile has got correct data.

This is my first WPF app and I have spent a week running myself in circles and trying to figure out what I am doing wrong, but to know avail, hence I am reaching out for guidance.

I dont know but I suspect I am not handling an event properly, not binding correctly or have done something wrong with DI.

Below I have provided the code from what I think are the main components.

What I want to have happen is the First Name of the logged in user is displayed inthe TextBlock of the ShellView after the user has Logged in as well as in the UserProfileView.

Any help you can offer would be appreciated to point me in the right direction. I have include what I think are the main components below.

ShellViewModel

using Caliburn.Micro;
using CRMDesktopUI.EventModels;
using CRMDesktopUI.Library.Models;
using System.Threading;
using System.Threading.Tasks;

namespace CRMDesktopUI.ViewModels
{
    public class ShellViewModel:Conductor<object>, IHandle<LogOnEvent>
    {
        private IEventAggregator _events;
        private SimpleContainer _container;
        private LoginViewModel _loginVM;
        private UserProfileViewModel _userProfileVM;
        private ILoggedInUserModel _loggedInUserModel;
        
        public ShellViewModel(LoginViewModel loginVM, IEventAggregator events,ILoggedInUserModel loggedInUserModel, UserProfileViewModel  userProfileVM,SimpleContainer container)
        {
            _events = events;
            _loginVM = loginVM;
            _userProfileVM = userProfileVM;
            _container = container;
            _loggedInUserModel = loggedInUserModel;
            _events.SubscribeOnUIThread(this);

            ActivateItemAsync(_loginVM);
        }

        Task IHandle<LogOnEvent>.HandleAsync(LogOnEvent message,CancellationToken cancellationToken)
        {
            _loginVM = _container.GetInstance<LoginViewModel>();
  
            ActivateItemAsync(_userProfileVM);
            
            return Task.CompletedTask;
        }

        public string FirstName
        {
            get  //This gets called before log in screen activated
            {
                if(_loggedInUserModel == null)
                {
                    return "Not logged in";
                }
                else
                {
                    return _loggedInUserModel.FirstName;
                }

            }
            set  //Set never gets called 
            {
                _loggedInUserModel.FirstName = value;
                NotifyOfPropertyChange(() => FirstName);
            }

        }
       
    }
}

ShellView.xaml

<Window x:Class="CRMDesktopUI.Views.ShellView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CRMDesktopUI.Views"
        xmlns:viewmodels="clr-namespace:CRMDesktopUI.ViewModels"
        mc:Ignorable="d"
        Width="1250" Height="600" 
        Background="#36393F"
        ResizeMode="CanResizeWithGrip"
        AllowsTransparency="True"
        WindowStyle="None">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="200"/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Border Grid.ColumnSpan="2"
                Background="#252525"
                MouseDown="Border_MouseDown">
            <Grid HorizontalAlignment="Stretch">
                <Label Content="Test App"
                         Foreground="Gray"
                         FontWeight="SemiBold"
                         FontFamily="/Fonts/#Poppins"/>
                <StackPanel HorizontalAlignment="Right"
                            Orientation="Horizontal">
                    <Button Width="20" Height="20"
                            Content="           
  • Related