首页 > 解决方案 > 如何以编程方式从 AvalonDockManager 激活(被选中)文档?

问题描述

我已经从我的项目中的 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;
    }

标签: c#wpfavalondock

解决方案


终于找到了 !我发布结果是因为我认为我不会孤单……首先,我在文档视图定义中添加了一个新属性:

#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 由 AvalonDock 定义

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

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

推荐阅读