Abstract borders and text block in different style / template in WPF

First of all, I would like to add custom text blocks to my GUI at the lowest possible cost. For instance:<TextBlock style={StaticRessources myTextBlock}>Text</TextBlock>

Now I have the following frame style:

<Style x:Key="greenBox" TargetType="Border">
  <Setter Property="Background" Value="#00FF00"/>
  <Setter Property="CornerRadius" Value="10"/>
  <Setter Property="BorderThickness" Value="1"/>
  <Setter Property="BorderBrush" Value="Black"/>
  <Setter Property="Height" Value="40"/>
  <Setter Property="Width" Value="100"/>
</Style>

And I apply it as follows:

<Border Style="{StaticResource greenBox}">
  <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">Custom Text</TextBlock>
</Border>

My problem is that it needs 2 tags, and the properties set in TextBlock will be superfluous. I cannot figure out how to abstract both definitions into one element.

+3
source share
2 answers

where the Label takes effect:

<Style TargetType="Label" x:Key="greenLabel">    
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="Template">        
        <Setter.Value>            
            <ControlTemplate TargetType="Label">
                <Border Style="{StaticResource greenBox}">
                    <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </Border>                
            </ControlTemplate>            
        </Setter.Value>        
    </Setter>    
</Style>

<Label Style="{StaticResource greenLabel}">Custom Text</Label>

(according to your other question: if this is the only place you use this border style, you can, of course, include them directly in this frame without using an additional style)

+3
source
0

All Articles