title: Why Window.Visibility property is not being "getted"?
body: <p>Working with MVVM, I have a <code>MainView.xaml</code> and a corresponding <code>MainViewModel.cs</code>. I also have a user control, <code>HomeView.xaml</code>, as well as <code>HomeViewModel.cs</code>.</p>
  <p>Also, I have a <code>TaskBarService</code>, singleton, being injected on both <code>MainViewModel.cs</code> and <code>HomeViewModel.cs</code>. <code>MainView.Window.Visibility</code> is bound to <code>TaskBarService.WindowVisibility</code> via a property.</p>
  <p>On the <code>HomeView</code>, when the user clicks a button, <code>TaskBarService.WindowVisibility</code> is set to <code>Visibility.Hidden</code>, while calling <code>OnPropertyChanged</code> for that same property. But <code>MainView.Window.Visibility</code> never looks for the new value. I've set a breakpoint on the property setter and it is only triggered once, when <code>MainView</code> is instanced. What am I doing wrong?</p>
  <p><strong>App.xaml.cs</strong></p>
  <pre><code>public partial class App : Application
  {
      private readonly IHost _host;
      public App()
      {
          _host = CreateHostBuilder().Build();
      }
  
      private static IHostBuilder CreateHostBuilder(string[] args = null)
      {
          return Host.CreateDefaultBuilder(args)
              .AddServices()
              .AddStores()
              .AddViewModels()
              .AddViews();
      }
  
      protected override void OnStartup(StartupEventArgs e)
      {
          _host.Start();
  
  
          Window janela = _host.Services.GetRequiredService&lt;MainView&gt;();
          TaskBarController taskBarController = _host.Services.GetRequiredService&lt;TaskBarController&gt;();
          taskBarController.WindowVisibility = Visibility.Visible;
          base.OnStartup(e);
      }
  
      protected override async void OnExit(ExitEventArgs e)
      {
          await _host.StopAsync();
          _host.Dispose();
  
          base.OnExit(e);
      }
  }
  </code></pre>
  <p><strong>App.xaml</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;Application x:Class=&quot;&lt;mynamespace&gt;.App&quot;
           xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
           xmlns:local=&quot;clr-namespace:&lt;mynamespace&gt;&quot;
           xmlns:viewmodels=&quot;clr-namespace:&lt;mynamespace&gt;.ViewModels&quot;
           xmlns:views=&quot;clr-namespace:&lt;mynamespace&gt;.Views&quot;&gt;
  &lt;Application.Resources&gt;
      &lt;DataTemplate DataType=&quot;{x:Type viewmodels:HomeViewModel}&quot;&gt;
          &lt;views:HomeView/&gt;
      &lt;/DataTemplate&gt;
      &lt;Style TargetType=&quot;{x:Type Rectangle}&quot;/&gt;
  
  &lt;/Application.Resources&gt;
  &lt;/Application&gt;
  </code></pre>
  <p><strong>AddServices</strong></p>
  <pre><code>public static class AddServicesHostBuilderExtensions
  {
      public static IHostBuilder AddServices(this IHostBuilder host)
      {
          host.ConfigureServices((context, services) =&gt;
          {
             services.AddSingleton&lt;TaskBarController&gt;();
  
          });
          return host;
      }
  }
  </code></pre>
  <p><strong>AddStores</strong></p>
  <pre><code> public static class AddStoresHostBuilderExtensions
  {
      public static IHostBuilder AddStores(this IHostBuilder host)
      {
          host.ConfigureServices((context, services) =&gt;
          {
              services.AddSingleton&lt;INavigator, Navigator&gt;();
          });
  
          return host;
      }
  }
  </code></pre>
  <p><strong>AddViewModels</strong></p>
  <pre><code>public static class AddViewModelsHostBuilderExtensions
  {
      public static IHostBuilder AddViewModels(this IHostBuilder host)
      {
          host.ConfigureServices((context, services) =&gt;
          {
              services.AddSingleton&lt;IViewModelFactory, ViewModelFactory&gt;();
  
              services.AddSingleton&lt;HomeViewModel&gt;();
              services.AddSingleton&lt;MainViewModel&gt;();
  
  
              services.AddSingleton&lt;CreateViewModel&lt;HomeViewModel&gt;&gt;(services =&gt; () =&gt; services.GetRequiredService&lt;HomeViewModel&gt;());
          });
          return host;
      }
  }
  </code></pre>
  <p><strong>AddViews</strong></p>
  <pre><code>public static class AddViewsHostBuilderExtensions
  {
      public static IHostBuilder AddViews(this IHostBuilder host)
      {
          host.ConfigureServices((context, services) =&gt; {
              services.AddSingleton&lt;MainView&gt;(createMainView);
          });
          return host;
      }
      private static MainView createMainView(IServiceProvider services)
      {
          return new MainView(services.GetRequiredService&lt;MainViewModel&gt;());
      }
  }
  </code></pre>
  <p><strong>ViewModelBase</strong></p>
  <pre><code>public delegate TViewModel CreateViewModel&lt;TViewModel&gt;() where TViewModel : ViewModelBase;
  public class ViewModelBase : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(string propertyName)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
  
  }
  </code></pre>
  <p><strong>MainView.xaml</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;Window x:Class=&quot;&lt;mynamespace&gt;.Views.MainView&quot;
      xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
      xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
      xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
      xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  
      Title=&quot;MainView&quot; WindowStartupLocation=&quot;CenterScreen&quot; ResizeMode=&quot;NoResize&quot; WindowStyle=&quot;None&quot;
      Height=&quot;250&quot; Width=&quot;400&quot; Visibility=&quot;{Binding WindowVisibility, Mode=TwoWay}&quot;&gt;
  &lt;Grid&gt;
      &lt;ContentControl Content=&quot;{Binding ViewModelAtual}&quot;/&gt;
  &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  <p><strong>MainView.xaml.cs</strong></p>
  <pre><code>public partial class MainView : Window
  {
      public MainView(object dataContext)
      {
          InitializeComponent();
          DataContext = dataContext;
      }
  }
  </code></pre>
  <p><strong>HomeView.xaml</strong></p>
  <pre class="lang-xml prettyprint-override"><code>&lt;UserControl x:Class=&quot;&lt;mynamespace&gt;.Views.HomeView&quot;
           xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
           xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
           xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot; 
           xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;&gt;
  &lt;Grid&gt;
      &lt;TextBlock Text=&quot;❌&quot;&gt;
          &lt;TextBlock.InputBindings&gt;
              &lt;MouseBinding Gesture=&quot;LeftClick&quot; Command=&quot;{Binding CloseWindow}&quot;/&gt;
          &lt;/TextBlock.InputBindings&gt;
      &lt;/TextBlock&gt;
  &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  <p><strong>Navigator.cs</strong></p>
  <pre><code>public class Navigator : INavigator
  {
      private ViewModelBase _viewModelAtual;
  
      public ViewModelBase ViewModelAtual
      {
          get { return _viewModelAtual; }
          set
          {
              _viewModelAtual = value;
              StateChanged?.Invoke();
          }
      }
  
      public event Action StateChanged;
  }
  </code></pre>
  <p><strong>ViewModelFactory.cs</strong></p>
  <pre><code>public class ViewModelFactory : IViewModelFactory
  {
      private readonly CreateViewModel&lt;HomeViewModel&gt; _createHomeViewModelFactory;
  
      public ViewModelFactory(CreateViewModel&lt;HomeViewModel&gt; createHomeViewModelFactory)
      {
          _createHomeViewModelFactory = createHomeViewModelFactory;
      }
  
      public ViewModelBase CreateViewModel(TipoView tipoView)
      {
          switch (tipoView)
          {
              case TipoView.Home:
                  return _createHomeViewModelFactory();
              default:
                  throw new ArgumentException(&quot;The ViewType does not have a ViewModel.&quot;, &quot;viewType&quot;);
          }
      }
  }
  </code></pre>
  <p><strong>TaskBarController</strong></p>
  <pre><code>public class TaskBarController : INotifyPropertyChanged
  {
      private Visibility windowVisibility;
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      protected void OnPropertyChanged(string propertyName)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
      public Visibility WindowVisibility
      {
          get =&gt; windowVisibility;
          set
          {
              windowVisibility = value;
              OnPropertyChanged(nameof(WindowVisibility));
          }
      }
  
      public void MinimizeToTray()
      {
          WindowVisibility = Visibility.Hidden;
      }
  
      public void MaximizeFromTray()
      {
          WindowVisibility = Visibility.Visible;
      }
  }
  </code></pre>
  <p><strong>UpdateViewModelCommand.cs</strong></p>
  <pre><code>public class UpdateViewModelAtualCommand : ICommand
  {
      private readonly INavigator _navigator;
      private readonly IViewModelFactory _viewModelFactory;
  
      public UpdateViewModelAtualCommand(INavigator navigator, IViewModelFactory viewModelFactory)
      {
          _navigator = navigator;
          _viewModelFactory = viewModelFactory;
      }
  
      public event EventHandler CanExecuteChanged;
  
      public bool CanExecute(object parameter)
      {
          return true;
      }
  
      public void Execute(object parameter)
      {
          if (parameter is TipoView)
          {
              TipoView tipoView = (TipoView)parameter;
  
              _navigator.ViewModelAtual = _viewModelFactory.CreateViewModel(tipoView);
          }
      }
  }
  </code></pre>
  <p><strong>MainViewModel.cs</strong></p>
  <pre><code>private readonly IViewModelFactory _viewModelFactory;
          private readonly INavigator _navigator;
          private TaskBarController _taskBarController;
  
          public ViewModelBase ViewModelAtual =&gt; _navigator.ViewModelAtual;
          public MainViewModel(INavigator navigator,
              IViewModelFactory viewModelFactory, TaskBarController taskBarController)
          {
              _taskBarController = taskBarController;
              _navigator = navigator;
              _viewModelFactory = viewModelFactory;
  
              _navigator.StateChanged += _navigator_StateChanged;
  
              UpdateViewModelAtual = new UpdateViewModelAtualCommand(navigator, _viewModelFactory);
              UpdateViewModelAtual.Execute(TipoView.Home);
  
          }
  
          private void _navigator_StateChanged()
          {
              OnPropertyChanged(nameof(ViewModelAtual));
          }
  
          public ICommand UpdateViewModelAtual { get; set; }
          public Visibility WindowVisibility
          {
              get =&gt; _taskBarController.WindowVisibility;
              set
              {
                  _taskBarController.WindowVisibility = value;
                  OnPropertyChanged(nameof(WindowVisibility));
              }
          }
  </code></pre>