WPF custom textbox

Source: Internet
Author: User

1. Add an icon in front of textbox.

Effect:

<TextBox Width="300" Height="30" Style="{StaticResource TXTSTYLE}">   <TextBox.Background>        <ImageBrush ImageSource="uri" />   </TextBox.Background>
</TextBox>
<Style  x:Key="TXTSTYLE" TargetType="{x:Type TextBox}">            <Setter Property="Template">                <Setter.Value>                    <ControlTemplate TargetType="{x:Type TextBox}">                        <Grid>                            <Grid.ColumnDefinitions>                                <ColumnDefinition Width="40" />                                <ColumnDefinition />                            </Grid.ColumnDefinitions>                            <Label Background="{TemplateBinding Background}"/>                            <Border Grid.Column="1" x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" SnapsToDevicePixels="true">                                <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>                            </Border>                        </Grid>                    </ControlTemplate>                </Setter.Value>            </Setter>        </Style>

2. rounded textbox.

 <Border BorderBrush="#FF989CA1" BorderThickness="1" Height="24" Width="240" CornerRadius="15">            <TextBox Margin="10,0" Text="TextBox" BorderBrush="{x:Null}" BorderThickness="0,1,1,1"  Style="{StaticResource TXTSTYLE}">                <TextBox.Background>                    <ImageBrush ImageSource="uri" />                </TextBox.Background>            </TextBox>        </Border>

The code in the second paragraph is in application. resources defines a style named txtstyle, which is modified by elements such as grid, label, border, and scrollviewer. when applied to Textbox, the icon position in front of the text box appears. the background attribute inserts an image into the textbox. The first code is the implementation code.

Based on the implementation of the image position, the code in the third section uses the border element to draw the rounded corner effect and set the textbox border attribute to achieve the rounded corner effect.

:

 

WPF custom textbox

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.