所以我得到了一个带有一些 Tabitems 和几个按钮的 TabControl。
当我按下这些按钮时,会改变 TabStrip 的位置。
一切正常。我现在想做的是,
当我按下按钮 Right90 将标签条向右放置时,标题应转动 90°。
我试着去做,但总是失败。
我会很高兴得到一个快速的答案。
这是我的代码:
<Window x:Class="Pages.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TabControl" Height="350" Width="525">
<Window.Resources>
<SolidColorBrush x:Key="Foreground" Color="#FF9531b1"></SolidColorBrush>
<SolidColorBrush x:Key="Background" Color="Transparent"></SolidColorBrush>
<ControlTemplate x:Key="TabItemControlTemplate" TargetType="{x:Type TabItem}">
<Grid SnapsToDevicePixels="True">
<Border x:Name="Bd" BorderBrush="{StaticResource Foreground}" BorderThickness="0" Padding="{TemplateBinding Padding}">
<Border.Style>
<Style TargetType="{x:Type Border}">
<Setter Property="Background" Value="{StaticResource Background}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsSelected}" Value="True">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsMouseOver}" Value="True">
<Setter Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Background}" />
</DataTrigger>
</Style.Triggers>
</Style>
</Border.Style>
<ContentPresenter x:Name="Content" ContentTemplate="{TemplateBinding HeaderTemplate}" Content="{TemplateBinding Header}" ContentStringFormat="{TemplateBinding HeaderStringFormat}" ContentSource="Header" HorizontalAlignment="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{Binding VerticalContentAlignment, RelativeSource={RelativeSource FindAncestor, AncestorLevel=1, AncestorType={x:Type ItemsControl}}}"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Bd" Property="Background" Value="{StaticResource Foreground}"></Setter>
<Setter TargetName="Content" Property="TextBlock.Foreground" Value="White"></Setter>
<Setter TargetName="Content" Property="TextBlock.FontWeight" Value="Heavy"></Setter>
</Trigger>
<MultiTrigger >
<MultiTrigger.Conditions >
<Condition Property="IsSelected" Value="True"></Condition>
<Condition Property="IsMouseOver" Value="false"></Condition>
</MultiTrigger.Conditions>
<Setter TargetName="Content" Property="TextBlock.Foreground" Value="{StaticResource Foreground}"></Setter>
<Setter TargetName="Content" Property="TextBlock.FontWeight" Value="Heavy"></Setter>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="1">
<Button x:Name="btnTop" Click="btnTop_Click">Top</Button>
<Button Name="btnRight" Click="btnRight_Click">Right</Button>
<Button Name="btnBottom" Click="btnBottom_Click">Bottom</Button>
<Button Name="btnLeft" Click="btnLeft_Click">Left</Button>
<Button Name="btnLeft90" Click="btnLeft90_Click">Left90</Button>
<Button Name="btnRight90" Click="btnRight90_Click">Right90</Button>
</StackPanel>
<TabControl Grid.Column="0" x:Name="MyControl" Height="200"
Background="Transparent"
BorderThickness="0,1,0,0"
BorderBrush="{StaticResource Foreground}" TabStripPlacement="Top">
<TabItem x:Name="Tab1" Header="tab1" Background="Transparent" Template="{DynamicResource TabItemControlTemplate}">
</TabItem>
<TabItem x:Name="Tab2" Header="tab2" Background="Transparent" Template="{DynamicResource TabItemControlTemplate}"/>
<TabItem x:Name="Tab3" Header="tab3" Background="Transparent" Template="{DynamicResource TabItemControlTemplate}"/>
</TabControl>
</Grid>
以及代码背后的代码:
public MainWindow()
{
InitializeComponent();
}
private void btnTop_Click(object sender, RoutedEventArgs e)
{
MyControl.TabStripPlacement = Dock.Top;
}
private void btnRight_Click(object sender, RoutedEventArgs e)
{
MyControl.TabStripPlacement = Dock.Right;
}
private void btnBottom_Click(object sender, RoutedEventArgs e)
{
MyControl.TabStripPlacement = Dock.Bottom;
}
private void btnLeft_Click(object sender, RoutedEventArgs e)
{
MyControl.TabStripPlacement = Dock.Left;
}
private void btnLeft90_Click(object sender, RoutedEventArgs e)
{
MyControl.TabStripPlacement = Dock.Left;
}
private void btnRight90_Click(object sender, RoutedEventArgs e)
{
MyControl.TabStripPlacement = Dock.Right;
Tab1.LayoutTransform = Rotation.Rotate90;
}
}
提前致谢