I have a normal C# tab control. I want to execute a function when the user right-clicks in the empty area to the right of the right-most tab header (anywhere in the tab header area that's not a tab is ok too).
I looked at positioning a hidden button in the empty area, but it doesn't fill the space and is a very hard-coded approach.
I also looked at creating a hidden tab in the empty area, but it too won't fill the empty space completely and the required header width is dependent on how many other tabs are showing; in addition, users might accidentally click on it and navigate away from the current tab, which would not be good because things happen automatically on tab changes.
Is there an elegant way to capture a right-click in the empty area?
I ended up doing it by setting a style with an event handler like so:
<Style x:Key="{dxt:DXTabControlInternalThemeKey ResourceKey=PanelContainerTopLayoutStyle, IsThemeIndependent=True}" TargetType="{x:Type dx:TabPanelContainer}" BasedOn="{StaticResource {dxt:DXTabControlInternalThemeKey ResourceKey=PanelContainerTopLayoutStyle}}">
<EventSetter Event="PreviewMouseRightButtonDown" Handler="DXTabControl_PreviewMouseRightButtonDown"/>
<Setter Property="Background" Value="Transparent"/>
</Style>
Then I looked in the event handler to see if there was a parent object like this:
private void tabControl_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) {
DXTabItem parentObject = DevExpress.Xpf.Core.Native.LayoutHelper.FindParentObject<DXTabItem>(e.OriginalSource as DependencyObject);
// If the click happened over an empty area (i.e., no parent object was found), then toggle the admin button
if (parentObject == null) {
if (tabItem_Admin.Visibility == System.Windows.Visibility.Visible)
tabItem_Admin.Visibility = System.Windows.Visibility.Collapsed;
else
tabItem_Admin.Visibility = System.Windows.Visibility.Visible;
}
}
This solution uses the DevExpress libraries and is courtesy of the excellent DevExpress support team!