Cascading styles in WPF (a la CSS)

there is a way to specify something like this in WPF:

CSS

#someSpan input { color: #f1f1f1; } or span input { color: #f1f1f1; } 

Meaning, I would like all the TextBlock elements inside the container to match the x style, without using a style for each text block.

just to clarify, I need to do something similar in WPF.

I know about the BasedOn attribute of the style .. but this is not quite what I am looking for here

looking for something like this

  <Style x:Key="FileItem" TargetType="{x:Type #SomeContainer TextBlock}"> 

or maybe in SomeContainer, add a TextBlock style that will apply to all its text blocks

+6
source share
2 answers

As for the last part of your question, if you want to apply a style to all TextBlock within a specific element, just put Style in these element resources:

 <TextBlock /> <!-- unaffected --> <Grid> <Grid.Resources> <Style TargetType="TextBlock"> <!-- ... --> </Style> </Grid.Resources> <TextBlock /> <!-- will be styled --> </Grid> 

If you have styles stored in a separate ResourceDictionary , you can "import" all of them for a specific element by combining resource dictionaries:

 <Grid> <Grid.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Resources/MyOtherStyles.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Grid.Resources> <TextBlock /> <!-- will be styled --> </Grid> 
+4
source

You can do this, you just need to nest the styles, for example.

 <Style TargetType="{x:Type Border}"> <Style.Resources> <Style TargetType="{x:Type TextBox}"> <!-- ... --> </Style> <Style.Resources> </Style> 

This allows you to style TextBoxes on TextBoxes , however elements can only have one style applied to them, so parallel "rules" will not work either.

+6
source

Source: https://habr.com/ru/post/900693/


All Articles