WPF - Overlapping Custom Tabs in a TabControl and ZIndex

Posted by Rachel on Stack Overflow See other posts from Stack Overflow or by Rachel
Published on 2010-03-26T16:44:33Z Indexed on 2010/05/11 17:44 UTC
Read the original article Hit count: 1206

Filed under:
|
|
|

Problem
I have a custom tab control using Chrome-shaped tabs that binds to a ViewModel. Because of the shape, the edges overlap a bit. I have a function that sets the tabItem's ZIndex on TabControl_SelectionChanged which works fine for selecting tabs, and dragging/dropping tabs, however when I Add or Close a tab via a Relay Command I am getting unusual results. Does anyone have any ideas?

Default View
http://i193.photobucket.com/albums/z197/Lady53461/tabs_default.jpg

Removing Tabs
http:/i193.photobucket.com/albums/z197/Lady53461/tabs_removing.jpg

Adding 2 or more Tabs in a row
http:/i193.photobucket.com/albums/z197/Lady53461/tabs_adding.jpg

Code to set ZIndex

private void PrimaryTabControl_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.Source is TabControl)
        {
            TabControl tabControl = sender as TabControl;
            ItemContainerGenerator icg = tabControl.ItemContainerGenerator;
            if (icg.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
            {
                foreach (object o in tabControl.Items)
                {
                    UIElement tabItem = icg.ContainerFromItem(o) as UIElement;
                    Panel.SetZIndex(tabItem, (o == tabControl.SelectedItem ? 100 :
                        90 - tabControl.Items.IndexOf(o)));
                }
            }
        }
    }

By using breakpoints I can see that it is correctly setting the ZIndex to what I want it to, however the layout is not displaying the changes. I know some of the changes are in effect because if none of them were working then the tab edges would be reversed (the right tabs would be drawn on top of the left ones). Clicking a tab will correctly set the zindex of all tabs (including the one that should be drawn on top) and dragging/dropping them to rearrange them also renders correctly (which removes and reinserts the tab item). The only difference I can think of is I am using the MVVM design pattern and the buttons that Add/Close tabs are relay commands.

Does anyone have any idea why this is happening and how I can fix it??

p.s. I did try setting a ZIndex in my ViewModel and binding to it, however the same thing happens when adding/removing tabs via the relay command.

EDIT: Being a new user I couldn't post images and could only post 1 link. Images just show a picture of what the tags render as after each scenario. Adding more then 1 at a time will not reset the zindex of other recently-added tabs so they go behind the tab on the Right, and closing tabs does not correctly render the ZIndex of the SelectedTab that replaces it and it shows up behind the tab on its right.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about zindex