WPF 自定义TextBox 前加图标和圆角 转载

1.TextBox前加图标

效果:

1

2

3

4

"300" Height="30" Style="{StaticResource TXTSTYLE}">

   

        "uri" />

   

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

"TXTSTYLE" TargetType="{x:Type TextBox}">

            "Template">

                

                    "{x:Type TextBox}">

                        

                            

                                "40" />

                                

                            

                            

                            "1" x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{DynamicResource {x:Static SystemColors.WindowBrushKey}}" SnapsToDevicePixels="true">

                                "PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>

                            

                        

                    

                

            

        

2.圆角TextBox。

1

2

3

4

5

6

7

"#FF989CA1" BorderThickness="1" Height="24" Width="240" CornerRadius="15">

           "10,0" Text="TextBox" BorderBrush="{x:Null}" BorderThickness="0,1,1,1"  Style="{StaticResource TXTSTYLE}">

               

                   "uri" />

               

           

       

  在第二段代码在Application.Resources中定义了一个名为TXTSTYLE的样式,由Grid、Label、Border、ScrollViewer等元素进行修饰,应用于TextBox时使文本框前面位置出现图标位置,通过设置TextBox.Background属性,将图片插入TextBox,第一段代码为实现代码。

      第三段代码在实现图片位置基础上,使用Border元素绘制圆角效果,设置TextBox边框属性,达到圆角效果。

效果图:

WPF 自定义TextBox 前加图标和圆角 转载_第1张图片

你可能感兴趣的:(WPF)