Question

Is there a way to change the TabIndex (in XAML only) when a button is clicked. Below is my try following this article and one on my own which is commented. Thanks!

<StackPanel>
    <Button x:Name="Button" Content="Go" />
    <TabControl x:Name="Tab">
        <TabItem Header="First">
            <TextBlock Text="First" />
        </TabItem>
        <TabItem Header="Second">
            <TextBlock Text="Second" />
        </TabItem>
        <TabControl.Triggers>
            <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonDown">
                <BeginStoryboard>
                    <Storyboard>
                        <Int32AnimationUsingKeyFrames
                            Duration="0"
                            Storyboard.TargetName="Tab"
                            Storyboard.TargetProperty="TabIndex">
                            <DiscreteInt32KeyFrame
                                KeyTime="0"
                                Value="1" />
                        </Int32AnimationUsingKeyFrames>
                        <!--<Int32Animation
                            Duration="0"
                            Storyboard.TargetName="Tab" 
                            Storyboard.TargetProperty="TabIndex"
                            FillBehavior="HoldEnd"
                            By="1"
                            From="0"
                            To="1" />-->
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </TabControl.Triggers>
    </TabControl>
</StackPanel>

Edit: Use SelectedIndex instead of TabIndex to change the current tab.

Was it helpful?

Solution

Your event trigger is misplaced. Instead of placing the trigger on TabControl, place trigger on StackPanel.

<StackPanel.Triggers>
    <EventTrigger RoutedEvent="Button.PreviewMouseLeftButtonDown">
        <BeginStoryboard>
            <Storyboard>
               <Int32AnimationUsingKeyFrames Duration="0"
                                             Storyboard.TargetName="Tab"
                                            Storyboard.TargetProperty="TabIndex">
                   <DiscreteInt32KeyFrame KeyTime="0"
                                          Value="1" />
               </Int32AnimationUsingKeyFrames>
            </Storyboard>
         </BeginStoryboard>
    </EventTrigger>
</StackPanel.Triggers>

Reason:

Button.PreviewMouseLeftButtonDown is a tunnelling routed attached event i.e. it will start from root element and continue to tunnel until it reaches actual sender. So, it will start from window or UserControl and will continue to tunnel until it reaches Go button. It won't tunnel to TabControl since it is sibling of button and not a parent of TabControl.

But it will work if you place it on StackPanel since StackPanel is parent of TabControl and Button. Before tunnelling to GO Button, it will pass via StackPanel. Hence, your storyBoard will work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top