简体   繁体   English

当另一个选项卡禁用时,如何使用 XAML 触发器来更改 TabControl 索引?

[英]How to use XAML triggers to change TabControl Index when another tab disables?

What I have is a TabControl in a WPF application with two tabs.我拥有的是带有两个选项卡的 WPF 应用程序中的 TabControl。 There is a collection called Voices , which you can see on line 6 as Path=GameMaster.Voices.Count .有一个名为Voices的集合,您可以在第 6 行看到它为Path=GameMaster.Voices.Count What I am trying to achieve is that when the collection is empty, the second tab disables, which I have done successfully.我想要实现的是,当集合为空时,第二个选项卡将禁用,我已成功完成。 Then, I would like the TabControl's SelectedIndex to be set to 0. This then not only disables the second tab, but also forces the user back to the first tab.然后,我希望将 TabControl 的 SelectedIndex 设置为 0。这不仅会禁用第二个选项卡,还会强制用户返回到第一个选项卡。 With the code below, the result works somewhat, however, when I add my first item to the collection, the tab index seems to set to -1.使用下面的代码,结果有些工作,但是,当我将我的第一个项目添加到集合中时,选项卡索引似乎设置为 -1。

Here is a screenshot of the TabControl upon application startup, when no objects are in the collection:这是应用程序启动时 TabControl 的屏幕截图,当集合中没有对象时:

初始视图

However, upon adding an item, the following occurs:但是,在添加项目时,会发生以下情况:

添加项目后查看

Here is my XAML code:这是我的 XAML 代码:

<TabControl Grid.Row="2" Margin="0,3,0,0">
    <TabControl.Resources>
        <Style TargetType="TabControl">
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Path=GameMaster.Voices.Count, Converter={StaticResource CountGreaterThanZeroConverter}}"
                    Value="False">
                    <Setter Property="SelectedIndex" Value="0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.Resources>

    <TabItem Header="Add new voice">

        <!-- TAB 1 CODE -->

    </TabItem>

    <TabItem Header="Edit Voices"
             Name="EditTab"
             IsEnabled="{Binding Path=GameMaster.Voices.Count, Converter={StaticResource CountGreaterThanZeroConverter}}">

        <!-- TAB 2 CODE -->

    </TabItem>
</TabControl>

And this is the Converter I am using:这是我正在使用的转换器:

internal class CountGreaterThanZeroConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var count = System.Convert.ToInt32(value);

        return count > 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

I have tried to understand what is going on, however I haven't found a suitable solution yet, and any help would be very much appreciated.我试图了解发生了什么,但是我还没有找到合适的解决方案,任何帮助将不胜感激。 Thank you.谢谢你。

Answering here, however, I am not 100% on why this has solved my issue.然而,在这里回答,我不是 100% 的为什么这解决了我的问题。 So, if someone could please let me know what the heck is going on, that would be a great asset to my knowledge!所以,如果有人可以让我知道到底发生了什么,这对我的知识来说将是一笔巨大的财富!

I have added the following line of code:我添加了以下代码行:

<TabControl Grid.Row="2" Margin="0,3,0,0">
    <TabControl.Resources>
        <Style TargetType="{x:Type TabControl}">
            <Setter Property="SelectedIndex" Value="0"/>    <!-- This line -->
            <Style.Triggers>
                <DataTrigger
                    Binding="{Binding Path=GameMaster.Voices.Count, Mode=OneWay, Converter={StaticResource CountGreaterThanZeroConverter}}"
                    Value="False">
                    <Setter Property="SelectedIndex" Value="0" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TabControl.Resources>

        ...

</TabControl>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM