1
 <xcad:DockingManager Name="myDockingManager" AnchorablesSource="{Binding FooterTools}" DocumentsSource="{Binding MainWindowTools}"
                            ActiveContent="{Binding ActiveDocument, Mode=TwoWay, Converter={StaticResource ActiveDocumentConverter}}"
                            avBehav:AvalonDockLayoutSerializer.LoadLayoutCommand="{Binding AdLayout.LoadLayoutCommand}"                                 avBehav:AvalonDockLayoutSerializer.SaveLayoutCommand="{Binding AdLayout.SaveLayoutCommand}">

在另一个地方,我得到了要加载默认布局的命令:

视图模型命令:

  private RelayCommand _loadDefaultLayoutCommand;
    public RelayCommand LoadDefaultLayoutCommand
    {
        get 
        {
            return _restoreDefaultLayoutCommand ?? (_loadDefaultLayoutCommand= new RelayCommand(() => LoadDefaultLayout())

            ); 
        }
    }


private object LoadDefaultLayout()
    {
       ;//AdLayout.LoadDefaultLayoutCommand.Execute();
       //unfortunately I cannot invoke above command since I need to convey AvalonDockManager as a parameter which is impossible since ViewModel does not know it.

    }

我得到了这样定义的 AvalonDockViewModel 属性:

  public AvalonDockLayoutViewModel AdLayout
    {
        get { return _avalonDockLayout ?? (_avalonDockLayout = new AvalonDockLayoutViewModel()); }
    }

我想从 ViewModel 调用命令来更新 View 我认为行为可以用作 View 和 ViewModel 之间的通信桥梁。

对于加载布局和保存,我使用了行为,例如,对于加载布局,我使用了如下行为:

private static void OnLoadLayoutCommandChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
    {
        var framworkElement = obj as FrameworkElement;    
        // Remove the handler if it exist to avoid memory leaks

        if (framworkElement != null)
        {
            framworkElement.Loaded  -= OnFrameworkElementLoaded;

            var command = e.NewValue as ICommand;
            if (command != null)
            {
                // the property is attached so we attach the Drop event handler
                framworkElement.Loaded += OnFrameworkElementLoaded;
            }
        }
    }

这是很合乎逻辑的,因为我有 frameworkElement Load 事件。

是否可以使用行为机制或其他机制按需加载默认布局,例如在我的情况下使用菜单命令?

4

0 回答 0