【问题标题】:Global Style in UWP XAMLUWP XAML 中的全局样式
【发布时间】:2017-08-27 22:17:33
【问题描述】:

我在 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 中也能正常工作?

【问题讨论】:

    标签: c# xaml uwp uwp-xaml


    【解决方案1】:

    不幸的是,没有,就像您的情况一样,它将被ItemsControl 的Foreground 覆盖。因此,您必须将以下内容添加到您的 App.xaml。

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

    当您处理更高级的ItemsControl(例如支持ItemContainerStyle 的ListView)时,您需要将TargetType 设置为其项目容器(即ListViewItem)。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-24
      • 2021-01-24
      • 1970-01-01
      • 2019-01-17
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      相关资源
      最近更新 更多