我显然在这里遗漏了一些东西。我试图在WPF窗口的tabcontrol中获取一个文本块。我从互联网上获取了一些示例代码并稍微改了一下,现在文本块没有显示。
修改 这种造型似乎是负责任的
<Window.Resources>
<Style TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Grid>
<Border
Name="Border"
Background="{DynamicResource TabSelected}"
BorderBrush="Black"
BorderThickness="1,1,1,1"
CornerRadius="6,6,0,0" >
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"/>
</Border>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource TabSelected}" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="{DynamicResource TabNormal}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TabControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TabPanel
Grid.Row="0"
Panel.ZIndex="1"
Margin="0,0,4,-1"
IsItemsHost="True"
Background="Transparent" />
<Border
Grid.Row="1"
BorderBrush="Black"
BorderThickness="1"
CornerRadius="0, 12, 12, 12" >
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
结束修改
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="25"></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition Height="25"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Content="Button" Height="23" Grid.Row="0" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TextBox Height="27" HorizontalAlignment="Left" Grid.Row="2" Name="txtMessages" VerticalAlignment="Top" Width="200" />
<TabControl Grid.Row="1" Grid.Column="0" Height="258" HorizontalAlignment="Left" Margin="12,15,0,0" Name="tabMainContent" VerticalAlignment="Top" Width="351">
<TabItem Header="Assigned Printers" Name="tabInstalledPrinters" Margin="0">
<TabItem.Content>
<StackPanel>
<Grid>
<Grid.RowDefinitions >
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Name="txtDisplayText" Height="23" Width="209" Text="Im Here" Background="Aqua" Margin="20,40,30,50" />
</Grid>
</StackPanel>
</TabItem.Content>
</TabItem>
<TabItem Header="Options" Name="tabOptions">
<Grid />
</TabItem>
</TabControl>
</Grid>
一切都如我所料,但Textblock没有显示,我已经做了Aqua以确保我没有错过它。
任何人都可以看到它没有显示的原因吗?
答案 0 :(得分:2)
您从ContentPresenter
样式中删除了TabControl
。把它放在Border
:
<Style TargetType="{x:Type TabControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabControl}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TabPanel Grid.Row="0" Panel.ZIndex="1" Margin="0,0,4,-1" IsItemsHost="True" Background="Transparent" />
<Border Grid.Row="1" BorderBrush="Black" BorderThickness="1" CornerRadius="0, 12, 12, 12" >
<ContentPresenter ContentSource="SelectedContent"/>
</Border>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>