【发布时间】:2016-08-13 08:56:21
【问题描述】:
这是第一个使用 WPF 和样式的项目。 开发工具为VS2010。
我有以下样式定义:
<!-- Base Style Setting-->
<Style TargetType="{x:Type TabControl}" x:Key="TabControlBase">
<Setter Property="Height" Value="Auto" />
<Setter Property="Width" Value="Auto" />
</Style>
<Style TargetType="{x:Type TabItem}" x:Key="TabItemBase">
<Setter Property="Cursor" Value="Hand" />
</Style>
<!-- Sub Style Setting-->
<Style TargetType="{x:Type TabItem}" x:Key="LTabEquipment" BasedOn="{StaticResource TabItemBase}">
<Setter Property="Header">
<Setter.Value>
<StackPanel Orientation="Vertical">
<Image Height="36" Source="pack://application:,,,/Images/gears.png" Width="36" Margin="0,5,0,0" />
<TextBlock Text="Equipment" Margin="2,0,0,0" VerticalAlignment="Bottom" HorizontalAlignment="Center" />
</StackPanel>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TabItem}" x:Key="LTabHistory" BasedOn="{StaticResource TabItemBase}">
<Setter Property="Header">
<Setter.Value>
<StackPanel Orientation="Vertical">
<Image Height="36" Source="pack://application:,,,/Images/history.png" Width="36" Margin="0,5,0,0" />
<TextBlock Text="History" Margin="2,0,0,0" VerticalAlignment="Bottom" HorizontalAlignment="Center" />
</StackPanel>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type TabItem}" x:Key="HTabDBase" BasedOn="{StaticResource TabItemBase}">
<Setter Property="Header">
<Setter.Value>
<StackPanel Orientation="Horizontal">
<Image Height="14" Source="pack://application:,,,/Images/dbase.png" Width="14" Margin="0,0,0,0" HorizontalAlignment="stretch" VerticalAlignment="stretch" />
<TextBlock Text="grid view" Margin="2,0,0,0" VerticalAlignment="center" HorizontalAlignment="center" />
</StackPanel>
</Setter.Value>
</Setter>
</Style>
然后在我的 MainWindow.xaml 中包含:
<TabControl Grid.Row="2" Name="tabLeft" TabStripPlacement="Left">
<TabItem x:Name="tabLeftEquipment" Style="{StaticResource LTabEquipment}">
<TabControl Style="{StaticResource TabControlBase}" Name="tabTopEquipment" >
<TabItem x:Name="tabTopEquipmentGridView" Style="{StaticResource HTabDBase}">
</TabItem>
</TabControl>
</TabItem>
<TabItem x:Name="tabLeftHistory" Style="{StaticResource LTabHistory}">
<TabControl Height="Auto" Name="tabTopHistory" Width="Auto">
<TabItem x:Name="tabTopHistoryGridView" Style="{StaticResource HTabDBase}" >
</TabItem>
</TabControl>
</TabItem>
在设计时样式提供了我想要的外观,但在运行时出现异常:
'设置属性'System.Windows.FrameworkElement.Style'抛出异常。'
哪个指向线:
<TabItem x:Name="tabTopEquipmentGridView" Style="{StaticResource HTabDBase}">
请帮助查找异常原因...谢谢!
我详细找到了以下内容,但仍然无法确定是哪个元素导致问题更正。
Message=Specified element is already the logical child of another element. Disconnect it first.
【问题讨论】:
-
你确定图片源路径在运行时正确吗?
-
到目前为止我得到的最好的线索是它与
或 HTabDBase 被应用在多个位置有关。 -
@ashur668 我已经按原样使用了您的代码,并且运行良好。我在
Window.Resources中定义了所有样式。而且您必须在代码中执行其他操作。 -
同意,我过度简化了我的原始条目。当在多个 TabItem 中使用 HTabDBase 样式时,该问题出现在 mainfest。
-
我从 MainWindow.xaml 修改了上面的代码示例。
标签: wpf