如何以编程方式从AvalonDockManager激活(选择)文档?

时间:2019-01-23 07:55:25

标签: c# wpf avalondock

我已经从项目中的AvalonDock内容创建了一个DockingManager,并且我的请求非常简单:当我将文档添加到LayoutDocumentPaneGroup时,我希望它处于活动状态,被选中,并且不仅添加到LayoutDocumentPaneGroup的末尾,还可以激活在第一个文件上。

我尝试为documentView类实现一个“ IsActive”属性,但是它不起作用。

我在xaml文件中的对接管理器定义如下:

<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>

我的documentView是使用以下类定义的:

公共抽象类DockWindowViewModel:BaseViewModel     {         #region属性

    #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;
    }

1 个答案:

答案 0 :(得分:0)

终于找到了!我发布结果是因为我想我不会一个人... 首先,我向文档视图定义添加一个新属性:

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

但是我还必须在我的XAML代码中将其实现为属性,如下所示:

<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>

假定它对在那里定义的LayoutContent的所有属性都起作用:LayoutDocument defined by AvalonDock

编辑: 还需要添加“ Mode = TwoWay”以通过编程方式在选定内容更改时进行更新,如下所示:

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