如何以编程方式从AvalonDock Manager激活(被选中)文档?

问题描述 投票:1回答:1

我在项目中从AvalonDock内容创建了一个DockingManager,我的请求非常简单:当我向LayoutDocumentPaneGroup添加文档时,我希望它是Active,Selected,而不仅仅是在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 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;
    }
c# wpf avalondock
1个回答
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}" />
© www.soinside.com 2019 - 2024. All rights reserved.