nikotromus
nikotromus

Reputation: 1054

rounded border on textblock issue

I am trying to give my textblocks rounded borders. From what I've researched, I should embed a textblock within a border tag and then set the corner radius of the border. For me, it's having the effect of putting a border around the entire row. What am I doing wrong? I set the color of the border to blue to show what is happening. Ideally, I would change it to the same color as the background of the textblock to just give it seamless rounded corners.

<Grid>                
   <Grid.RowDefinitions>
       <RowDefinition Height="Auto"></RowDefinition>                   
   </Grid.RowDefinitions>


   <Border Grid.Row="0" Margin="25" Grid.Column="1" Background="Blue"
           BorderThickness="1" BorderBrush="Red"  CornerRadius="30">
           <TextBlock  Margin="50" Padding="200,0,200,0"
                       FontSize="100"
                       FontWeight="Bold"
                       Background="Black"
                       Foreground="White"    
                       VerticalAlignment="Center"
                       Text="bla bla bla"
                       HorizontalAlignment="Center">
           </TextBlock>
    </Border>
</Grid>

Upvotes: 0

Views: 122

Answers (2)

jclake
jclake

Reputation: 101

A better way to do this is to create a copy of the default style for TextBox and modify the border in the ControlTemplate. Here is a simple app that has the default TextBox style extracted and modified to round the corners.

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
        <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>
        <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" CornerRadius="5">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                        <Condition Property="IsSelectionActive" Value="false"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox Margin="10" Style="{DynamicResource TextBoxStyle1}" Text="bla bla bla"></TextBox>
        </StackPanel>
    </Grid>
</Window>

If you want this style to apply to all textboxes in your app, you should move the style into a resource dictionary and remove x:Key="TextBoxStyle1" from the style definition. It would then apply to all TextBox by default and remove the need to set a Style for each one.

Upvotes: 1

Peregrine
Peregrine

Reputation: 4546

Look at the HorizontalAlignment / VerticalAlignment values for both the Grid and the Border. Set whatever is most appropriate for your requirements.

A tool such as Kaxaml is great for playing around with this sort of thing without having to build an entire application.

Upvotes: 2

Related Questions