我是WPF的新手,尝试打开tabcontrol所在的表单时遇到以下错误:
'TabItem'ControlTemplate TargetType与模板化类型'TabControl'不匹配。
这是我在网格中的tabcontrol:
let epoch: TimeInterval = Double(1532127600000/1000.0)
let lDate = Date(timeIntervalSince1970: epoch);
print(lDate)
let dateFomatter = DateFormatter()
dateFomatter.timeZone = TimeZone(identifier: "America/Los_Angeles")
dateFomatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ"
let lDateString = dateFomatter.string(from: lDate)
print(lDateString)
let convertedDate = dateFomatter.date(from: lDateString)
print(convertedDate!)
这是我为其定义的样式:
<Grid>
<TabControl Style="{DynamicResource MyTabControl}" HorizontalAlignment="Left" BorderThickness="0"
Height="549" Margin="10,10,0,0" Background="LightGray"
VerticalAlignment="Top" Width="509">
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
有什么问题吗?
答案 0 :(得分:0)
为了通过样式设置TabItem的ControlTemplate,样式必须使用TabItem
而不是TabControl
作为其TargetType:
<Style x:Key="MyTabItemStyle" TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
...
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
除此之外,还必须将样式应用于TabControl的ItemContainerStyle
而不是其Style
:
<TabControl ItemContainerStyle="{DynamicResource MyTabItemStyle}" ...>
...
</TabControl>
答案 1 :(得分:0)
如消息所示,目标类型错误。 改变这个
<Style x:Key="MyTabControl" TargetType="{x:Type TabControl}">
到
<Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
您可以这样做:
<Window.Resources>
<Style x:Key="TabItemStyle1" TargetType="{x:Type TabItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TabItem}">
<Border Name="Border" BorderThickness="1,1,1,0"
BorderBrush="Gainsboro"
CornerRadius="4,4,0,0" Margin="2,0">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightSkyBlue"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
并使用
<TabControl>
<TabItem Header="TabItem1" Style="{DynamicResource TabItemStyle1}">
</TabItem>
<TabItem Header="TabItem2" Style="{DynamicResource TabItemStyle1}">
</TabItem>
</TabControl>
将产生以下输出: