【问题标题】:Xaml Style Child Control in Universal Windows Platform (UWP)通用 Windows 平台 (UWP) 中的 Xaml 样式子控件
【发布时间】:2016-07-29 14:47:47
【问题描述】:

我想知道如何在样式定义中在 UWP 上设置样式定位子控件。

WPF 似乎有 'Style.Resources' 来定义子样式,但 UWP 似乎并非如此

wpf 中的示例:WPF - How to create a style that applies styles to child types

【问题讨论】:

  • 你可以把它们扔进Whatever.Resources中,它们会被继承。

标签: xaml resources styles controls win-universal-app


【解决方案1】:

如果您希望将样式放在单独的工作表中(您应该这样做。我在控件本身中显示了样式,因为我误读并认为这是您想要的),您可以创建一个 Resources 文件夹并添加不同的 ResourceDictionaries。我个人通常为画笔、样式、模板和转换器创建一个单独的字典。然后在 App.xaml 的 ResourceDictionary.MergedDictionaries 中声明它们

  <Application
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
RequestedTheme="Light">
<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
          <ResourceDictionary Source="/Resources/Brushes.xaml" />
          <ResourceDictionary Source="/Resources/Styles.xaml" />
            <ResourceDictionary Source="/Resources/Converters.xaml" />
            <ResourceDictionary Source="/Resources/Templates.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

【讨论】:

  • 抱歉不清楚我是 xaml 的新手,我理解那部分。让我们将您的 Styles.xaml 放在 MergedDictionaries 中,我将如何为自定义控件及其子控件定义样式。以您之前的 StackPanel 示例为例,我想您会有一个 StackPanel 作为 TargetType 的 Style,然后将 StackPanel 上的 Style 设置为 StaticResource,但是在其中定义的矩形呢?使用 Style.Resources,我似乎可以在我的 Styles.xaml 中获得自定义控件的样式层次结构,但 UWP 中没有,这就是我想要的。感谢您的宝贵时间。
【解决方案2】:

您可以在父控件的 ResourceDictionary 中定义样式。 Window.Resources 中定义的 Style 适用于所有 Rectangle,因为它没有指定 Key,所以第一个 StackPanel 中的 Rectangle 是黄色的并且很小。第二个 StackPanel 定义了它自己的资源,它的子级使用这些资源,它们最终有不同的颜色和更大的尺寸。还有使用 BasedOn 的样式继承

<Window x:Class="GBTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-GBTest"
    mc:Ignorable="d"
    Title="MainWindow"
    Height="350"
    Width="525">
<!--Default style defined in the Window Resources-->
<Window.Resources>
    <Style TargetType="Rectangle">
        <Setter Property="Width"
                Value="100" />
        <Setter Property="Height" Value="100" />
        <Setter Property="Fill"
                Value="Yellow" />
    </Style>
</Window.Resources>
<StackPanel>
    <Rectangle />
    <StackPanel>
        <!--The styles defined in the resources of this StckPanel will apply to its children, overriding the default style defined in the Window Resources-->
        <StackPanel.Resources>
            <Style TargetType="Rectangle"
                   x:Key="BigStyle">
                <Setter Property="Height"
                        Value="200" />
                <Setter Property="Width"
                        Value="200" />
            </Style>
            <Style TargetType="Rectangle"
                   x:Key="RedStyle"
                   BasedOn="{StaticResource BigStyle}">
                <Setter Property="Fill"
                        Value="Red" />
            </Style>
            <Style TargetType="Rectangle"
                   BasedOn="{StaticResource BigStyle}"
                   x:Key="BlueStyle">
                <Setter Property="Fill"
                        Value="Blue" />
            </Style>
        </StackPanel.Resources>

        <Rectangle Style="{StaticResource RedStyle}" />
        <Rectangle Style="{StaticResource BlueStyle}" />
    </StackPanel>
</StackPanel>

【讨论】:

  • 感谢您的重播,虽然这可以完成工作,但这仍然意味着我正在定义控件本身的样式。我想要的是拥有完全分离的样式和控件。有点像 CSS,我在一个单独的文件中定义所有内容。这就是为什么我认为 Style.Resources 很不错
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 2017-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多