WPF中的层叠样式(一个CSS)

Son*_*oul 6 css wpf styling

有没有办法在WPF中指定这样的东西:

CSS:

#someSpan input { color: #f1f1f1; }
or
span input { color: #f1f1f1; }
Run Code Online (Sandbox Code Playgroud)

意思是,我希望容器中的所有TextBlock元素都遵循x样式,而不必将样式应用于每个文本块.

为了澄清,我需要在WPF中做这样的事情.

我知道一个样式的BasedOn属性..但这不是我在这里寻找的东西

寻找这样的东西

 <Style x:Key="FileItem"  TargetType="{x:Type #SomeContainer TextBlock}">
Run Code Online (Sandbox Code Playgroud)

或者也许在SomeContainer中,添加将应用于其所有文本块的TextBlock样式

Ste*_*rex 6

关于问题的最后一部分,如果您想将样式应用于TextBlock特定元素中的所有 s,只需将 s 放入Style该元素资源中即可:

<TextBlock /> <!-- unaffected -->

<Grid>
    <Grid.Resources>
        <Style TargetType="TextBlock">
            <!-- ... -->
        </Style>
    </Grid.Resources>

    <TextBlock /> <!-- will be styled -->
</Grid>
Run Code Online (Sandbox Code Playgroud)

如果您将样式存储在单独的位置ResourceDictionary,那么您可以通过合并资源字典来“导入”特定元素的所有样式:

<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary Source="/Resources/MyOtherStyles.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>

    <TextBlock /> <!-- will be styled -->
</Grid>
Run Code Online (Sandbox Code Playgroud)


H.B*_*.B. 6

你可以这样做,你只需要嵌套样式,例如

<Style TargetType="{x:Type Border}">
    <Style.Resources>
        <Style TargetType="{x:Type TextBox}">
            <!-- ... -->
        </Style>
    <Style.Resources>
</Style>
Run Code Online (Sandbox Code Playgroud)

这使你的风格TextBoxesBorders,但是元素只能应用了一种风格,所以具有平行的"规则"是不会正常工作.