全局样式
问题描述:
我在App.xaml中设置一些风格全局样式
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value ="HotPink"/>
</Style>
这种风格适用于正常对照组,而不是里面的DataTemplates
<TextBlock Text="Test"></TextBlock> <!-- Works here -->
<ItemsControl ItemsSource="{Binding ViewModel.UniverseGroups}" HorizontalAlignment="Right" VerticalAlignment="Center">
<ItemsControl.ItemTemplate>
<DataTemplate>
<RelativePanel>
<TextBlock Text="{Binding Name}"></TextBlock>
<!-- This text still is black -->
</RelativePanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
有没有一种方法,使全球样式甚至可以在DataTemplates中工作?
答
不幸的是,没有,如在你的情况下,它将被的Foreground
覆盖。所以你必须在你的App.xaml
中添加以下内容。
<Style TargetType="ItemsControl">
<Setter Property="Foreground" Value ="HotPink"/>
</Style>
当你正在处理更高级的ItemsControl
像ListView
支持ItemContainerStyle
,你需要将TargetType
设置为其项目的容器(即ListViewItem
)来代替。
<Style TargetType="ListViewItem">
<Setter Property="Foreground" Value ="HotPink"/>
</Style>
对我来说也行得通! – AntiHeadshot