2017-08-27 99 views
2

我在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中工作?

回答

4

不幸的是,没有,如在你的情况下,它将被的Foreground覆盖。所以你必须在你的App.xaml中添加以下内容。

<Style TargetType="ItemsControl"> 
    <Setter Property="Foreground" Value ="HotPink"/> 
</Style> 

当你正在处理更高级的ItemsControlListView支持ItemContainerStyle,你需要将TargetType设置为其项目的容器(即ListViewItem)来代替。

<Style TargetType="ListViewItem"> 
    <Setter Property="Foreground" Value ="HotPink"/> 
</Style> 
+2

对我来说也行得通! – AntiHeadshot