我从SampleDockWindowView创建了一个视图,并且我想在按下按钮时转到另一个视图创建的窗口。
我已经尝试过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 e.g., 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> <xcad:DockingManager DocumentsSource="{Binding DocumentsMainPool}"> <xcad:DockingManager.LayoutItemTemplate> <DataTemplate DataType="{x:Type local:Document}"> <TextBlock Text="{Binding Title}" /> </DataTemplate> </xcad:DockingManager.LayoutItemTemplate> <xcad: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> </xcad:DockingManager.LayoutItemContainerStyle> <xcad:LayoutRoot> <xcad:LayoutPanel> <xcad:LayoutDocumentPaneGroup> <!--*** Dynamically created content (by view model) ***--> <xcad:LayoutDocumentPane /> </xcad:LayoutDocumentPaneGroup> </xcad:LayoutPanel> </xcad:LayoutRoot> </xcad:DockingManager> </Grid> </UserControl>
MainViewModel.cs
class MainViewModel : INotifyProeprtyChanged { public MainViewModel() { this.DocumentsMainPool = new ObservableCollection<IDocument>() { new Document("First Document"), new Document("Second Document") }; } private ObservableCollection<IDocument> documentsMainPool; public ObservableCollection<IDocument> DocumentsMainPool { get => this.documentsMainPool; set { this.documentsMainPool = value; OnPropertyChanged(); } } public ICommand NavigateToNextDocument => new RelayCommand(param => CycleNextDocuments()); private void CycleNextDocuments() { // Only one or no document -> nothing to cycle through if (this.DocumentsMainPool.Count < 2) { return; } IDocument currentlySelectedDocument = this.DocumentsMainPool.FirstOrDefault(document => document.IsSelected); int currentDocumentIndex = this.DocumentsMainPool.IndexOf(currentlySelectedDocument); // If last document reached, show first again if (currentDocumentIndex == this.DocumentsMainPool.Count - 1) { this.DocumentsMainPool.FirstOrDefault().IsSelected = true; return; } IDocument nextDocument = this.DocumentsMainPool .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
public interface IDocument : INotifyPropertyChanged { string Title { get; set; } bool IsSelected { get; set; } }
Document.cs
取自Microsoft Docs: Patterns - WPF Apps With The Model-View-ViewModel Design Pattern:的实现public class Document : IDocument { public Document(string title) { this.Title = title; } #region Implementation of IDocument private string title; public string Title { get => this.title; set => this.title = value; } 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
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
}