Category / Section
How to bind custom commands to built-in context menu items of document tabitem in WPF DockingManager?
1 min read
WPF DockingManager has individual routed event to notify the TabItem closing by below context menu items of DocumentTabItem,
1)Close
2)CloseAll
3)CloseAllButThis
Using commands to notify those actions is recommended if the application has MVVM structure. DockingManager has built-in support to bind the custom commands for those ContextMenu actions. The following code example demonstrate the same,
Mainwindow.xaml:
<syncfusion:DockingManager syncfusion:DockingManagerCloseOtherTabsCommand.Command="{Binding CloseAllButThis}" syncfusion:DockingManagerCloseOtherTabsCommand.CommandParameter="{Binding Path=ActiveWindow,ElementName=Docking}" syncfusion:DockingManagerCloseAllTabsCommand.Command="{Binding CloseAll}" syncfusion:DockingManagerCloseAllTabsCommand.CommandParameter="{Binding Path=ActiveWindow,ElementName=Docking}" syncfusion:DockingManagerCloseButtonClickCommand.Command="{Binding Close}" syncfusion:DockingManagerCloseButtonClickCommand.CommandParameter="{Binding Path=ActiveWindow,ElementName=Docking}" UseDocumentContainer="True" Name="Docking"> <ContentControl Name="Dock1" syncfusion:DockingManager.State="Document" syncfusion:DockingManager.Header="Dock1" ></ContentControl> <ContentControl syncfusion:DockingManager.Header="Dock2" syncfusion:DockingManager.State="Document" ></ContentControl> <ContentControl syncfusion:DockingManager.Header="Dock3" syncfusion:DockingManager.State="Document"></ContentControl> </syncfusion:DockingManager>
Mainwindow.cs:
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); DataContext = this; } private ICommand close; public ICommand Close { get { if (close == null) { close = new DelegateCommand(CloseWindow, CanClose); } return close; } set { close = value; } } private bool CanClose(object arg) { return true; } private void CloseWindow(object obj) { MessageBox.Show(DockingManager.GetHeader(obj as FrameworkElement)+ " to be closed"); } private ICommand closeall; public ICommand CloseAll { get { if (closeall == null) { closeall = new DelegateCommand(CloseWindow, CanClose); } return closeall; } set { closeall = value; } } private ICommand closeallbutthis; public ICommand CloseAllButThis { get { if (closeallbutthis == null) { closeallbutthis = new DelegateCommand(CloseWindow, CanClose); } return closeallbutthis; } set { closeallbutthis = value; } } }