Question

I'm dragging data from outside my application onto a TabControl. I'd like to be able to drag over a "tab" and have that tab brought to the front. The drag events on the TabControl and the TabItems only seem to fire for the active tab, and only fire when dragging over the tab content, not the "tab" itself.

Drag to tab

The markup for the control is:

<Window
    x:Class="DragOverTabExample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow"
    Height="350"
    Width="525">
    <TabControl
        HorizontalAlignment="Stretch"
        VerticalAlignment="Stretch">
        <TabItem
            Header="Tab A">
            <TextBlock>Tab A</TextBlock>
        </TabItem>
        <TabItem
            Header="Tab B">
            <TextBlock>Tab B</TextBlock>
        </TabItem>
    </TabControl>
</Window>

I've tried adding behaviours to both the TabControl and the individual TabItems, below, attaching to the DragOver and DragEnter events, but as mentioned above, none seem to fire whilst dragging over the tab itself.

namespace Sample
{
    public class ActivateOnDragOverBehaviour : Behavior<TabControl>
    {
        protected override void OnAttached()
        {
            AssociatedObject.DragOver += ActivateTab;
        }

        private void ActivateTab(object sender, DragEventArgs e)
        {
            // Bring hovered tab to front
        }
    }
}
Was it helpful?

Solution

You have to set AllowDrop="True" on the TabControl in order for the events to fire

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top