Rounded corner of TextBox in WPF

I am browsing web text for Rounded TextBox and find the xaml code as below:

 <Style TargetType="{x:Type my1:CustomTextBox}">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate >
                        <Border Background="{TemplateBinding Background}" x:Name="Bd" 
BorderThickness="2" CornerRadius="5" BorderBrush="#FFF9EAB6">
                            ***<ScrollViewer x:Name="PART_ContentHost" />***
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="BorderBrush" Value="#FFC7B0B0"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter TargetName="Bd" Property="BorderBrush" Value="#FFC7B0B0"/>
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="False">
                                <Setter Property="Foreground" Value="#FFC7B0B0"/>
                            </Trigger>
                            <Trigger Property="Width" Value="Auto">
                                <Setter Property="MinWidth" Value="120"/>
                            </Trigger>
                            <Trigger Property="Height" Value="Auto">
                                <Setter Property="MinHeight" Value="27"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>

                </Setter.Value>
            </Setter>
        </Style>

I want to know what is

<ScrollViewer x:Name="PART_ContentHost" />

and why it’s not right to work with my template, if you delete this line from it, please tell me in full.

Many thanks.

+5
source share
4 answers

The part with the name "PART_ContentHost" contains the control core, this is the text field itself, except for decorations. The textbox code behind will look for it, so if you rename delete, the control will not work. In this case, the content scrolls (since the text field can scroll text horizontally and vertically).

+6
source

, :

<Border Padding="5" CornerRadius="5" BorderThickness="1" BorderBrush="LightGray" SnapsToDevicePixels="True" Background="White">
    <TextBox Background="Transparent" BorderThickness="0">This is beautifull ;)</TextBox>
</Border>
+5

ScrollViewercontains the actual contents of the control. Your control is not a real text field, but actually a border (with rounded corners) surrounding the ScrollViewer, into which you will then need to place your text. If you do not need scrolling, you can replace ScrollViewer with a text field, that is:

change

<ScrollViewer x:Name="PART_ContentHost" />

to

<TextBox x:Name="PART_ContentHost" />
+1
source

use this part of xaml deign:

 <TextBox x:Name="usernameText" Height="30" Width="300"  TextWrapping="Wrap" Text="" FontSize="20" HorizontalContentAlignment="Center" LostFocus="usernameText_LostFocus">
                        <TextBox.Resources>
                            <Style TargetType="{x:Type Border}">
                                <Setter Property="CornerRadius" Value="10"/>
                            </Style>
                        </TextBox.Resources>
                    </TextBox>

The form will be like this:> </a> </p></div></body> </html>

+1
source

All Articles