1.TextBox前加圖標。
效果:
<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.圓角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>
在第二段代碼在Application.Resources中定義了一個名為TXTSTYLE的樣式,由Grid、Label、Border、ScrollViewer等元素進行修飾,應用於TextBox時使文本框前面位置出現圖標位置,通過設置TextBox.Background屬性,將圖片插入TextBox,第一段代碼為實現代碼。
第三段代碼在實現圖片位置基礎上,使用Border元素繪制圓角效果,設置TextBox邊框屬性,達到圓角效果。
效果圖: