Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set style for all textblocks in a stack panel

Tags:

styles

wpf

xaml

Let's say I have two different, distinct stack panels (we'll call them SPA and SPB), each with 10 textblocks as child elements. All the textblocks in SPA should use one style, and all the textblocks in SPB should use another. One way to accomplish this would be to declare the two styles in Resources, and then append Style="style1" to all 10 textblocks in the first stack panel, and append Style="style2" to all 10 in the second one. However, it seems like there should be some easy way to append a style to the stackpanel itself that somehow tells the stackpanel to apply it to all child elements that are textblocks. Is there anyway to do this?

The reason I naturally look for this solution is because this is exactly how you do the same sort of thing in HTML with CSS, and I was hoping there would be a similar feature to XAML with styling.

Thanks!

P.S. I am working with Silverlight, but I'm guessing my situation and whatever solution (if there is one) applies to XAML/WPF in general.

like image 230
JoeCool Avatar asked Feb 02 '11 17:02

JoeCool


People also ask

Where to put style in XAML?

The most common way to declare a style is as a resource in the Resources section in a XAML file. Because styles are resources, they obey the same scoping rules that apply to all resources. Put simply, where you declare a style affects where the style can be applied.

Can we use CSS in WPF?

We can easily find parallel concepts to these within WPF ... The only concept for which there really is no correspondent in WPF is CSS class. This can easily be introduced via an attached property.

What is stack panel?

StackPanel is a layout panel that arranges child elements into a single line that can be oriented horizontally or vertically. By default, StackPanel stacks items vertically from top to bottom in the order they are declared. You can set the Orientation property to Horizontal to stack items from left to right.


2 Answers

In the resources section for your main container put your style with a x:Key attribute and a target type of TextBlock. Then in each resources section for each StackPanel you can put a style where the BasedOn attribute is set to the key of your main style (don't forget to use StaticResource binding, not just the name of the key) and then say TargetType="{x:Type TextBlock}" and end the tag. this should bring the style into the StackPanel and style all of your TextBlocks.

<Window ...>     <Window.Resources>         <Style x:Key="tbstyle" TargetType="{x:Type TextBlock}">             <!-- put setters here -->         </Style>     </Window.Resources>     <StackPanel name="SPA">         <StackPanel.Resources>             <Style BasedOn="{StaticResource tbstyle}" TargetType="{x:Type TextBlock}" />         </StackPanel.Resources>         <TextBlock ... />         <TextBlock ... />         <TextBlock ... />         <TextBlock ... />         <TextBlock ... />     </Stackpanel>     <StackPanel name="SPB">         <StackPanel.Resources>             <Style BasedOn="{StaticResource tbstyle}" TargetType="{x:Type TextBlock}" />         </StackPanel.Resources>         <TextBlock ... />         <TextBlock ... />         <TextBlock ... />         <TextBlock ... />         <TextBlock ... />     </StackPanel> </Window> 
like image 183
Scott M. Avatar answered Oct 11 '22 21:10

Scott M.


<StackPanel>     <StackPanel.Resources>         <Style TargetType="TextBlock">             <Setter Property="Margin"                     Value="5" />         </Style>     </StackPanel.Resources>     <TextBlock Text="Text" />     <TextBlock Text="Text" />     <TextBlock Text="Text" />     <TextBlock Text="Text" /> </StackPanel> 
like image 44
decyclone Avatar answered Oct 11 '22 21:10

decyclone