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