gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:17:21 24 4
gpt4 key购买 nike

我从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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com