WPF DevComponents TabNavigation无法在代码隐藏中更改SelectedIndex / Item

时间:2010-07-19 22:39:07

标签: c# wpf selecteditem tabnavigator

我正在使用WPF的DevComponents TabNavigation控件,并且能够在特定索引处将TabItem添加到TabNavigation,在代码隐藏中将其称为i。现在我想通过执行以下操作使新的TabItem成为SelectedItem:

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;
}

然而,当我运行此代码时,不是将新的TabItem带入视图,而是将其置于视图中,然后我所在的原始选项卡会快速移回视图。

如果有人对于为什么会这样做有任何想法,以及我如何解决它,请告诉我。我附上了以下XAML的样本:

    <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>

提前致谢。

1 个答案:

答案 0 :(得分:0)

尝试在textBlock_MouseLeftButtonDown中将e。Handled设置为True。

我不熟悉那个控件,但是如果它像TabControl一样工作,那么它有一个逻辑可以在单击时将选项卡置于视图中。该逻辑看到原始选项卡被单击,并在更改后将其重新显示。将EventArgs对象标记为Handled将阻止WPF调用父元素上的事件处理程序,这将阻止选项卡切换回来。