简体   繁体   中英

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. There is a collection called Voices , which you can see on line 6 as 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. 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.

Here is a screenshot of the TabControl upon application startup, when no objects are in the collection:

初始视图

However, upon adding an item, the following occurs:

添加项目后查看

Here is my XAML code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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