c# - 当按下按钮或按键时,如何在AvalonDock中从一个选项卡转到另一个选项卡?

标签 c# wpf xaml mvvm avalondock

我从SampleDockWindowView创建了一个 View ,按下按钮时,我喜欢转到另一个 View 创建的窗口。

我已经尝试过Application.Current.Windows。该数组为空。

window2 win2= new window2();
        win2.Show();

我会像这样用avalondock选项卡成像。不一定是新窗口,而只是在按下按钮时显示现有窗口

最佳答案

下面的示例演示如何在按下Ctrl +右键组合时循环浏览自定义DocumentManager的所有文档选项卡(在此示例中,DocumentManager控件需要具有焦点):

MainWindow.xaml

<Window>
  <Window.DataContext>
    <local:MainViewModel />
  </Window.DataContext>

  <Grid>

    <!-- 
      Border to capture keyboard events of the DocumentManager UserControl. 
      To capture keys in a different scope i.e. more globally, 
      move the input bindings to a parent control.
    -->
    <Border>
      <!-- Bind keyboard keys to a ICommand. -->
      <Border.InputBindings>
        <KeyBinding Key="Right" 
                    Modifiers="Control" 
                    Command="{Binding NavigateToNextDocument}"/>
      </Border.InputBindings>

      <DocumentManager x:Name="DocumentManager" />
    </Border>
  </Grid
</Window>

DocumentManager.xaml
<UserControl>    
  <Grid>
    <xceed:DockingManager DocumentsSource="{Binding DocumentMainPool}">

      <xceed:DockingManager.LayoutItemTemplate>
        <DataTemplate DataType="{x:Type local:Document}">
          <TextBlock Text="{Binding Title}" />
        </DataTemplate>
      </xceed:DockingManager.LayoutItemTemplate>

      <xceed:DockingManager.LayoutItemContainerStyle>
        <Style TargetType="xcad:LayoutItem">
          <Setter Property="Title"
                  Value="{Binding Model.Title}" />
          <Setter Property="ToolTip"
                  Value="{Binding Model.Title}" />

          <!-- Important -->
          <Setter Property="IsSelected"
                  Value="{Binding Model.IsSelected, Mode=TwoWay}" />
        </Style>
      </xceed:DockingManager.LayoutItemContainerStyle>

      <xceed:LayoutRoot>
        <xceed:LayoutPanel>
          <xceed:LayoutDocumentPaneGroup>

            <!-- *** Dynamically created content (by view model) *** -->
            <xceed:LayoutDocumentPane />

          </xceed:LayoutDocumentPaneGroup>
        </xceed:LayoutPanel>
      </xceed:LayoutRoot>
    </xceed:DockingManager>

  </Grid>
</UserControl>

MainViewModel.cs
class MainViewModel : INotifyProeprtyChanged
{
  public MainViewModel()
  {
    this.DocumentMainPool = new ObservableCollection<IDocument>() 
    {
      new Document("First Document"), 
      new Document("Second Document")
    };
  }

  private ObservableCollection<IDocument> documentMainPool;
  public ObservableCollection<IDocument> DocumentMainPool
  {
    get => this.documentMainPool;
    set
    {
      this.documentMainPool = value;
      OnPropertyChanged();
    }
  }

  public ICommand NavigateToNextDocument => new RelayCommand(param => CycleNextDocuments());

  private void CycleNextDocuments()
  {
    // Only one or no document -> nothing to cycle through
    if (this.DocumentMainPool.Count < 2)
    {
      return;
    }

    IDocument currentlySelectedDocument = this.DocumentMainPool.FirstOrDefault(document => document.IsSelected);
    int currentDocumentIndex = this.DocumentMainPool.IndexOf(currentlySelectedDocument);

    // If last document reached, show first again
    if (currentDocumentIndex == this.DocumentMainPool.Count - 1)
    {
      this.DocumentMainPool.FirstOrDefault().IsSelected = true;
      return;
    }

    IDocument nextDocument = this.DocumentMainPool
      .Skip(currentDocumentIndex + 1)
      .Take(1)
      .FirstOrDefault();
    nextDocument.IsSelected = true;
  }

  public event PropertyChangedEventHandler PropertyChanged;
  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  {
    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }
}

IDocument.cs
// Important: IsSelected must raise PropertyChanged
public interface IDocument : INotifyPropertyChanged
{
  string Title { get; set; }
  bool IsSelected { get; set; }
}

Document.cs
public class Document : IDocument
{
  public Document(string title)
  {
    this.Title = title;
  }

  #region Implementation of IDocument

  public string Title { get; set; }

  private bool isSelected;
  public bool IsSelected
  {
    get => this.isSelected;
    set
    {
      this.isSelected = value;
      OnPropertyChanged();
    }
  }

  public event PropertyChangedEventHandler PropertyChanged;
  protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
  {
    this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  }

  #endregion
}

RelayCommand.cs
来自Microsoft Docs: Patterns - WPF Apps With The Model-View-ViewModel Design Pattern的实现:
public class RelayCommand : ICommand
{
    #region Fields 
    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;
    #endregion // Fields 
    #region Constructors 
    public RelayCommand(Action<object> execute) : this(execute, null) { }
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute; _canExecute = canExecute;
    }
    #endregion // Constructors 

    #region ICommand Members 
    [DebuggerStepThrough]
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    public void Execute(object parameter) { _execute(parameter); }
    #endregion // ICommand Members 
}

关于c# - 当按下按钮或按键时,如何在AvalonDock中从一个选项卡转到另一个选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60228182/

相关文章:

c# - 在 Windows Phone 上实现可滚动表

c# - EF 迁移 - 在迁移期间使用 C# 业务逻辑修改现有数据

c# - C# 中的条件变量作用域

c# - 有方法的属性?

wpf - 在 wpf 中以编程方式添加主机控件时,命令绑定(bind)不起作用

c# - wpf 详细信息主视图示例

c# - 如何使用 Selenium 遍历非选择下拉列表?

wpf - 触发器和数据触发器有什么区别?

c# - 在 ItemsControl DataTemplate 中设置 Canvas 属性

xaml - 如何在 Xamarin.Forms UWP 中使用图像?