我正在编写一个选项卡式应用程序,其中我使用带有4个选项卡项的tabcontrol,我在选项卡4中有一个按钮,当我点击它时我想返回第一个选项卡,我尝试了TabControl.SelectedIndex = 0但是似乎它不起作用,你能帮忙吗?
这里是xaml
<TabControl x:Name="TabAdd" HorizontalAlignment="Left" Height="535" VerticalAlignment="Top" Width="645" BorderBrush="#FFE8E8E8" Padding="0" IsSynchronizedWithCurrentItem="True" IsTextSearchEnabled="True">
<TabItem x:Name="TabAddFlat" Header="Flat View" TabIndex="0"><!--IsSelected="True"-->
<Grid Background="#FFFBFBFB" Margin="0">
<Grid x:Name="GridAddFlat" HorizontalAlignment="Left" Height="466" VerticalAlignment="Top" Width="407" Margin="109,10,0,0">
<Image HorizontalAlignment="Left" Height="466" VerticalAlignment="Top" Width="274" Source="Resources/F18.large - Copy.png" Margin="3,0,0,0"/>
</Grid>
</Grid>
</TabItem>
<TabItem x:Name="TabAddSide" Header="Side View" TabIndex="1">
<TabItem.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFFBFBFB" Offset="1"/>
</LinearGradientBrush>
</TabItem.Background>
<Grid Background="#FFFBFBFB">
<Grid x:Name="GridAddSide" HorizontalAlignment="Left" Height="345" VerticalAlignment="Top" Width="444" Margin="119,49,0,0">
<Image HorizontalAlignment="Left" Height="343" VerticalAlignment="Top" Width="394" Source="Resources/F1.large.png"/>
</Grid>
</Grid>
</TabItem>
<TabItem TabIndex="3" x:Name="TabAddInfo" Header="Customer Info" HorizontalAlignment="Left" Height="45" VerticalAlignment="Top" Width="184" Margin="0,0,-78,0">
<TabItem.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFE8E8E8" Offset="1"/>
</LinearGradientBrush>
</TabItem.Background>
<Grid Background="#FFFBFBFB">
<Grid x:Name="GridAddInfo" HorizontalAlignment="Left" Height="466" VerticalAlignment="Top" Width="407" Margin="109,10,0,0">
<TextBox x:Name="iaddress" MaxHeight="288" Height="95" Width="288" Controls:TextboxHelper.ClearTextButton="True" Controls:TextboxHelper.Watermark="Address" HorizontalAlignment="Center" VerticalAlignment="Center" Grid.IsSharedSizeScope="True" Padding="0" UseLayoutRounding="True" RenderTransformOrigin="0.5,0.5" Margin="27,246,92,125" AutoWordSelection="True" />
</Grid>
</Grid>
</TabItem>
<TabItem TabIndex="3" x:Name="TabAddReview" Header="Review" HorizontalAlignment="Left" Height="45" VerticalAlignment="Top" Width="106" GotFocus="LoadAddReview">
<TabItem.Background>
<LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
<GradientStop Color="#FFF3F3F3" Offset="0"/>
<GradientStop Color="#FFEBEBEB" Offset="0.5"/>
<GradientStop Color="#FFDDDDDD" Offset="0.5"/>
<GradientStop Color="#FFFBFBFB" Offset="1"/>
</LinearGradientBrush>
</TabItem.Background>
<Grid Background="#FFFBFBFB">
<Image HorizontalAlignment="Left" Height="32" Margin="15,15,0,0" VerticalAlignment="Top" Width="32" Source="Resources/save-64.png" MouseLeftButtonDown="SaveFormAll"/>
</Grid>
</Grid>
</TabItem>
</TabControl>
</Grid>
这是c#代码
private void SaveFormAll(object sender, MouseButtonEventArgs e)
{
TabAdd.SelectedIndex = 2;
}
答案 0 :(得分:2)
在您用作按钮的图像上,将MouseLeftButtonDown =“SaveFormAll”更改为MouseLeftButtonUp =“SaveFormAll”,然后重试。这应该有用。
问题似乎是你的MouseLeftButtonDown处理程序正在触发,但MouseLeftButtonUp事件紧跟在它之后并将焦点放回图像上,这意味着最后一个标签保持活动状态。当我将其更改为使用MouseLeftButtonUp时,它可以工作。