wpfavalondockdocking

AvalonDock: Dynamically changing LayoutAnchorablePane's CanClose property


In AvalonDock, is it possible to modify a LayoutAnchorablePane's CanClose property dynamically at runtime? I would like to prevent/lock layout changes unless the user specifically desires to do so and turns it on.

I have tried the following approaches:

  1. Binding: It is not possible to bind the property because it is not a DependencyProperty so something like this doesn't work: <dock:LayoutAnchorable CanClose="{Binding CanClose}">
  2. CanClose property: Changing the LayoutAnchorablePane's CanClose property in code-behind is not possible because the property is read-only.

Solution

  • According to the source code of LayoutAnchorablePane:

    #region CanClose
    
    public bool CanClose
    {
      get
      {
        return Children.All( a => a.CanClose );
      }
    }
    
    #endregion
    

    the CanClose property depends on all children of the pane, so one way to change the value of CanClose of a LayoutAnchorablePane is to set all its children's CanClose property to the value you want. Below is an example:

    ...
    foreach(var child in pane.Children)
    {
        child.CanClose = true; // or false.
    }
    ...