title: Load and save layout of anchorables - Binding of Visibility
body: <p>I am facing the problem that I cannot open an anchorable of type X after I have loaded my old layout. This happens only when I have closed the anchorable of type X before saving the layout.</p>
  
  <p>Does anyone have a similar problem with <a href="https://avalondock.codeplex.com/">AvalonDock</a>? Is this a bug of AvalonDock? After years of debugging, I fear that the binding <code>&lt;Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/&gt;</code> doesn't get updated correctly in the view, when changing <code>IsActive</code> in the ViewModel. AvalonDock should be responsible for this task. But maybe the problem is the loading and saving of the layout?</p>
  
  <h1>The code</h1>
  
  <p><strong>View</strong></p>
  
  <p>I am loading the saved layout of my anchorables (= tool windows) in the <code>Loaded</code> Event of my <a href="https://xceed.com/wp-content/documentation/xceed-toolkit-plus-for-wpf/Xceed.Wpf.AvalonDock~Xceed.Wpf.AvalonDock.DockingManager.html"><code>DockingManager</code></a> in my View like this (simplified):</p>
  
  <pre><code>string savedLayout = Properties.Settings.Default.Layout;
  XmlDocument doc = new XmlDocument();
  doc.LoadXml(savedLayout);
  
  // very simplified code. load saved xml layout and add anchorables to the dockmanager
  doc.SelectNodes("//LayoutAnchorable").OfType&lt;XmlNode&gt;().ToList().ForEach(anchorable =&gt;
  {
      this.DockManager.AnchorablesSource.Add(anchorable);
  });
  </code></pre>
  
  <p>I am saving the current layout of my anchorables in the <a href="https://msdn.microsoft.com/en-us/library/system.windows.window.closing.aspx"><code>Closing</code></a> Event of my <a href="https://msdn.microsoft.com/en-US/library/system.windows.window.aspx"><code>MainWindow</code></a> in my View like this (simplified):</p>
  
  <pre><code>XmlDocument doc = new XmlDocument();
  XmlLayoutSerializer xmlLayoutSerializer = new XmlLayoutSerializer(this.DockManager);
  
  using (MemoryStream stream = new MemoryStream())
  {
      xmlLayoutSerializer.Serialize(stream);
      stream.Seek(0, SeekOrigin.Begin);
      doc.Load(stream);
  }
  // here happens some magic. i think this code is not responsible for my problem
  Properties.Settings.Default.Layout = doc.OuterXml;
  </code></pre>
  
  <p>The ViewModel is bound to the ViewModel in the XAML like this (simplified):</p>
  
  <pre><code>&lt;xcad:DockingManager x:Name="DockManager" AnchorablesSource="{Binding Tools}" Loaded="DockManager_Loaded"&gt;
      &lt;xcad:DockingManager.LayoutItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type dockctrl:LayoutItem}"&gt;
              &lt;Setter Property="Title" Value="{Binding Model.ContentId}" /&gt;
              &lt;Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}" /&gt;
              &lt;Setter Property="CanClose" Value="{Binding Model.CanClose, Mode=TwoWay}" /&gt;
              &lt;Setter Property="Visibility" Value="{Binding Model.IsVisible, Mode=TwoWay, Converter={StaticResource Bool2vis}, ConverterParameter={x:Static Visibility.Hidden}}"/&gt;
              &lt;Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" /&gt;
              &lt;Setter Property="IconSource" Value="{Binding Model.IconSource}" /&gt;
              &lt;Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/&gt;
              &lt;Setter Property="ContentId" Value="{Binding Model.ContentId}" /&gt;
          &lt;/Style&gt;
      &lt;/xcad:DockingManager.LayoutItemContainerStyle&gt;
  [...]
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <p>The anchorable is opened in the ViewModel of the MainWindow. Here is the example code for the messages:</p>
  
  <pre><code>public ObservableCollection&lt;ToolBoxViewModelBase&gt; Tools { get; } = new ObservableCollection&lt;ToolBoxViewModelBase&gt;();
  
  public MainWindowViewModel()
  {
      // [...]
      this.MessagesWindow = new MessagesWindowViewModel();
      SimpleIoc.Default.Register&lt;MessagesWindowViewModel&gt;(() =&gt; this.MessagesWindow);
      this.ShowMessagesWindowCommand = new RelayCommand(() =&gt; this.OpenToolBox(this.MessagesWindow));
      // [...]
  }
  
  public void OpenToolBox&lt;T&gt;(T viewModel) where T : ToolBoxViewModelBase
  {
      // [...]
      viewModel.IsVisible = true;
      viewModel.IsActive = true;
      // [...]
  }
  </code></pre>
  
  <p>Just let me know if you need more information or wether i have missed to add some code!</p>