在TabItem之间切换

时间:2018-07-24 14:53:40

标签: c# wpf xaml

我对WPF和XAML还是很陌生。

使用下面的代码,我可以在2个TabItems之间切换,并且所选的标签将变为红色。 单击每个TabItem的文本时,我可以在各个选项卡之间切换,但是单击该选项卡上没有任何文本的部分时,例如框的右上方,则无法切换。

单击选项卡的“空白”部分时如何在选项卡之间切换?

谢谢。

<Window x:Class="testApp.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:testApp"
            mc:Ignorable="d"
            Title="testApp" Height="600" MinHeight="375" Width="900" MinWidth="600">

        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50px" />
                <ColumnDefinition Width="419*"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>

            <Border Grid.Row="0" Grid.Column="0" BorderThickness="0,0,0,0" Background="#4B6C8B" Grid.ColumnSpan="1" Grid.RowSpan="2">
                <Grid>
                    <TabControl Name="tabbie" HorizontalAlignment="Left" VerticalAlignment="Top" TabStripPlacement="Left" SelectionChanged="tabChanged">
                        <TabControl.Resources>
                            <Style TargetType="TabItem">
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="TabItem">
                                            <Border Name="Panel">
                                                <ContentPresenter Name="ContentSite" VerticalAlignment="Center" HorizontalAlignment="Center" ContentSource="Header" Margin="10, 2" />
                                            </Border>
                                            <ControlTemplate.Triggers>
                                                <Trigger Property="IsSelected" Value="True">
                                                    <Setter TargetName="Panel" Property="Background" Value="Red" />
                                                </Trigger>
                                            </ControlTemplate.Triggers>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </TabControl.Resources>
                        <TabItem Header="T1" Name="Test1" Width="51" Height="50px" HorizontalAlignment="Left" Margin="-3,-3,0,3" IsSelected="True" />
                        <TabItem Header="T2" Name="Test2" Width="51" Height="50px" HorizontalAlignment="Left" Margin="-3,-3,0,3" />
                    </TabControl>
                </Grid>
            </Border>

        </Grid>
    </Window>

0 个答案:

没有答案