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

发布于 2024-09-10 16:38:00 字数 2769 浏览 7 评论 0原文

我正在使用 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;
}

但是,当我运行此代码时,它不是进入视图,而是进入视图,然后我所在的原始选项卡快速移回到视图中看法。

如果有人对为什么会发生这种情况以及如何解决它有任何想法,请告诉我。我附上了下面的 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>

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欢烬 2024-09-17 16:38:00

尝试将 e.Handled 设置为 True textBlock_MouseLeftButtonDown。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文