Search code examples
c#wpfavalondock

How to programatically Activate (being Selected) a Document from AvalonDockManager?


I've created a DockingManager from AvalonDock content in my project and my request is quite simple: when I add a document to my LayoutDocumentPaneGroup I want it to be Active, Selected, and not only add at the end of the LayoutDocumentPaneGroup still activate on the first document.

I tried to implement an IsActive property to my documentView class but it doesn't work.

My dockingmanager in xaml file is defined as below:

<dock:DockingManager DataContext="{Binding DockManagerViewModel}" DocumentsSource="{Binding Documents}"  AnchorablesSource="{Binding Anchorables}">
    <dock:DockingManager.Resources>
    <!-- add views for specific ViewModels -->
        <DataTemplate DataType="{x:Type vmdock:SampleDockWindowViewModel}">
            <uscontrol:SampleDockWindowView />
        </DataTemplate>
    </dock:DockingManager.Resources>
    <dock:DockingManager.LayoutItemContainerStyle>
                            <Style TargetType="{x:Type dockctrl:LayoutItem}">
                                <Setter Property="Title" Value="{Binding Model.Title}" />
                                <Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
                                <Setter Property="CanClose" Value="{Binding Model.CanClose}" />
                            </Style>
                        </dock:DockingManager.LayoutItemContainerStyle>
                        <dock:LayoutRoot>
                            <dock:LayoutPanel Orientation="Vertical">
                                <dock:LayoutDocumentPaneGroup>
                                    <dock:LayoutDocumentPane />
                                </dock:LayoutDocumentPaneGroup>
                                <dock:LayoutAnchorablePaneGroup>
                                    <dock:LayoutAnchorablePane />
                                </dock:LayoutAnchorablePaneGroup>
                            </dock:LayoutPanel>
                        </dock:LayoutRoot>
                    </dock:DockingManager>

My documentView is defined with the class as below:

public abstract class DockWindowViewModel : BaseViewModel
{
    #region Properties

    #region CloseCommand
    private ICommand _CloseCommand;
    public ICommand CloseCommand
    {
        get
        {
            if (_CloseCommand == null)
                _CloseCommand = new RelayCommand(call => Close());
            return _CloseCommand;
        }
    }
    #endregion

    #region IsClosed
    private bool _IsClosed;
    public bool IsClosed
    {
        get { return _IsClosed; }
        set
        {
            if (_IsClosed != value)
            {
                _IsClosed = value;
                OnPropertyChanged(nameof(IsClosed));
            }
        }
    }
    #endregion

    #region CanClose
    private bool _CanClose;
    public bool CanClose
    {
        get { return _CanClose; }
        set
        {
            if (_CanClose != value)
            {
                _CanClose = value;
                OnPropertyChanged(nameof(CanClose));
            }
        }
    }
    #endregion

    #region Title
    private string _Title;
    public string Title
    {
        get { return _Title; }
        set
        {
            if (_Title != value)
            {
                _Title = value;
                OnPropertyChanged(nameof(Title));
            }
        }
    }
    #endregion

    #endregion

    public DockWindowViewModel()
    {
        CanClose = true;
        IsClosed = false;
    }

    public void Close()
    {
        IsClosed = true;
    }

Solution

  • Finally found ! I post the result because I think I'll not be alone... First, I add a new property to my document view definition:

    #region IsSelected
            private bool _isSelected = false;
            public bool IsSelected
            {
                get
                {
                    return _isSelected;
                }
                set
                {
                    if (_isSelected != value)
                    {
                        _isSelected = value;
                        OnPropertyChanged(nameof(IsSelected));
                    }
                }
            }
    #endregion
    

    But I had also to implement it as a property in my XAML code, as below:

    <dock:DockingManager.LayoutItemContainerStyle>
        <Style TargetType="{x:Type dockctrl:LayoutItem}">
            <Setter Property="Title" Value="{Binding Model.Title}" />
            <Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}" />
            <Setter Property="CanClose" Value="{Binding Model.CanClose}" />
            **<Setter Property="IsSelected" Value="{Binding Model.IsSelected}" />**
        </Style>
    </dock:DockingManager.LayoutItemContainerStyle>
    

    Supposed it works the same for all properties of LayoutContent defined there: LayoutDocument defined by AvalonDock

    EDIT : Need to add also "Mode=TwoWay" to programmatically update when the selected content changed, like this:

    <Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}" />