question

58901228 avatar image
0 Votes"
58901228 asked 58901228 commented

How to make all TextBlock objects contained in StackPanel use the same style in WPF?

         <StackPanel Grid.Column="0" Orientation="Vertical"
                     Style="{StaticResource Englishfont}">
           <TextBlock>h</TextBlock>
           <TextBlock>b</TextBlock>
           <TextBlock>tw</TextBlock>
           <TextBlock>tf</TextBlock>
           <TextBlock>N</TextBlock>
           <TextBlock>Mx</TextBlock>
           <TextBlock>My</TextBlock>
           <TextBlock>lox</TextBlock>
         </StackPanel>

I set it like this, but it didn't work.

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

DaisyTian-1203 avatar image
0 Votes"
DaisyTian-1203 answered 58901228 commented

You can set style for TextBox in <StackPanel.Resources> like below:

       <WrapPanel>
             <StackPanel Grid.Column="0" Orientation="Vertical">
                 <StackPanel.Resources>
                     <Style TargetType="TextBlock">
                         <Setter Property="Foreground" Value="Red"></Setter>
                     </Style>
                 </StackPanel.Resources>
                 <TextBlock>h</TextBlock>
                 <TextBlock>b</TextBlock>
                 <TextBlock>tw</TextBlock>
                 <TextBlock>tf</TextBlock>
                 <TextBlock>N</TextBlock>
                 <TextBlock>Mx</TextBlock>
                 <TextBlock>My</TextBlock>
                 <TextBlock>lox</TextBlock>
             </StackPanel>
        
             <TextBlock Name="txt2">My</TextBlock>
         </WrapPanel>

TextBoxs in StackPanel will have same style, but the TextBlock named txt2 doesn't.


If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for you answer.

0 Votes 0 ·