如何解决直接儿童的样式
鉴于此示例 XAML:
<TabControl>
<TabItem Header="Test">
<Grid> <!-- outer grid that should receive the styles -->
<Grid.RowDeFinitions><!-- ... --></Grid.RowDeFinitions>
<Grid.ColumnDeFinitions><!-- ... --></Grid.ColumnDeFinitions>
<Grid Grid.Row="1" Grid.Column="1">
<!-- inner grid,should NOT receive the styles -->
</Grid>
</Grid>
</TabItem>
</TabControl>
如何为 Grid
的所有直接 TabItem
子项设置样式,并且在层次结构中没有其他 Grid
的更深层次?
这是我尝试过的(我把它放在 App.xml
中):
<Style targettype="TabItem">
<Style.Resources>
<Style targettype="Grid">
<Setter Property="Margin" Value="10" />
</Style>
</Style.Resources>
</Style>
(我知道我可以使用 Style={StaticResource ...}
分配某些样式,但我必须将它单独应用于所有 Grid
,这似乎有很多不必要的代码......)
解决方法
如何为 Grid
的所有直接 TabItem
子项设置样式,并且在层次结构中没有其他 Grid
的更深层次?
通过以一种或另一种方式为所有外部 Style
元素显式设置 Grid
属性。
例如,您可以创建一个自定义 Grid
类型,将 Style
应用于:
public class OuterGrid : Grid { }
XAML:
<Style TargetType="local:OuterGrid">
<Setter Property="Margin" Value="10" />
</Style>
...
<local:OuterGrid>
<!-- outer grid that should receive the styles -->
<Grid Grid.Row="1" Grid.Column="1">
<!-- inner grid,should NOT receive the styles -->
<TextBlock>inner</TextBlock>
</Grid>
</local:OuterGrid>
或者在不使用 Grid
的情况下指定自定义 Style
的默认值:
public class OuterGrid : Grid
{
public OuterGrid()
{
Margin = new Thickness(10);
}
}
恐怕在 XAML 中没有 CSS 子选择器 (>
) 的概念。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。