views:

57

answers:

1

I am using the DevComponents TabNavigation control for WPF, and am able to add a new TabItem to the TabNavigation at a specific index, call it i, in the code-behind. Now I want to make the new TabItem the SelectedItem, by doing:

private void textBlock_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    int i = createNewTabItem(0, "Foo");

    TabNavigation tn = (((sender as TextBlock).Parent as Grid).Parent as TabItem).Parent as TabNavigation;
    tn.SelectedItem = tn.Items[i];
}

private int createNewTabItem(int overflowSrcPageNum, String header)
{
    TabItem ti = new TabItem();
    ti.Header = header;
    tabNavigation.Items.Insert(overflowSrcPageNum + 1, ti);
    return overflowSrcPageNum + 1;
}

When I run this code, however, instead of the new TabItem being brought into view, it is brought into view and then the original tab I was on is quickly moved back into view.

If anyone has any ideas as to why this is happening, and how I can fix it please let me know. I have attached a sample of the XAML below:

    <Grid >
        <Grid.Resources>
            <ResourceDictionary>
                <Style TargetType="TextBlock">
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="TextDecorations" Value="Underline"></Setter>
                        </Trigger>
                    </Style.Triggers>
                    <Setter Property="Foreground" Value="White" />
                    <Setter Property="FontFamily" Value="Segoe UI" />
                    <Setter Property="FontSize" Value="11" />
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="HorizontalAlignment" Value="Right" />
                    <Setter Property="Text" Value="View More..." />
                    <Setter Property="Visibility" Value="Visible" />
                    <EventSetter Event="MouseLeftButtonDown" Handler="lblMoreCpartys_MouseLeftButtonDown" />
                </Style>
            </ResourceDictionary>
        </Grid.Resources>
        <my:TabNavigation Background="Black" HorizontalAlignment="Stretch" Margin="0" Name="tabNavigation" 
                      VerticalAlignment="Stretch" MouseLeftButtonDown="tabNavigation_MouseLeftButtonDown"   
                      FontSize="12" Foreground="SteelBlue" ForceCursor="True" MouseWheel="tabNavigation_MouseWheel"
                      TabStripPlacement="Bottom">
            <TabItem Header="ITEM 1" Name="firstTabItem" FontSize="12" >
                    <TextBlock Name="firstTB" />
            </TabItem>
            <TabItem Header="ITEM 2" Name="secondTabItem" FontSize="12" >
                    <TextBlock Name="secondTB" />
            </TabItem>
        </my:TabNavigation>
    </grid>

Thanks in advance.

A: 

Try setting e.Handled to True in textBlock_MouseLeftButtonDown.

I'm not familiar with that control, but if it works like TabControl then it has logic to bring a tab into view when it is clicked. That logic sees that the original tab was clicked, and brings it back into view after your change. Marking the EventArgs object as Handled will stop WPF from calling event handlers on parent elements, which would stop the tab from switching back.

Quartermeister
Thank you! That worked perfectly.
Patrick K