首页 > 其他分享 >WPF TextBox搜索框&自定义TextBox样式

WPF TextBox搜索框&自定义TextBox样式

时间:2022-12-09 06:22:17浏览次数:56  
标签:自定义 样式 xaml 搜索 WPF 代码 TextBox

先看效果图咯:

 

 

 

前面的文章中,button样式告一段落。接下来分享几个TextBox样式。

后续持续更新中~

代码都在git上同步。有需要的可以下载查看。项目地址在之前的文章中都有写哦。

依旧是老规矩,话不多说,上代码咯。

首先要做搜索框当然要有一个搜索的图标啦,幸运的是,fontawesome里面有的~

在Fonts.xaml里面加上这个 图标资源

 

 

 <system:String x:Key="FontAwesomeSearch">&#xf002;</system:String>

 

 

显示出来就是上面这个。

在Texts.xaml里面写样式,代码如下 :

<Style x:Key="SearchTextBox" TargetType="TextBox">
        <Setter Property="FontSize" Value="{StaticResource FontSizeRegular}"/>
        <Setter Property="Width" Value="150"/>
        <Setter Property="Height" Value="40"/>
        <Setter Property="Foreground" Value="White"/>
        <Setter Property="CaretBrush" Value="White"/>
        <Setter Property="Text" Value="66"/>
        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Grid>
                        <Border x:Name="border" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="20">
                            <Grid>
                                <ScrollViewer x:Name="PART_ContentHost" 
                                              HorizontalAlignment="Left"
                                              VerticalAlignment="Center"
                                              Width="100"
                                              Margin="10 0 0 0"
                                              Focusable="False"
                                              HorizontalScrollBarVisibility="Hidden" 
                                              VerticalScrollBarVisibility="Hidden"/>
                                <Button Style="{StaticResource IconGrowButton}"
                                        Content="{StaticResource FontAwesomeSearch}" 
                                        HorizontalAlignment="Right"/>
                            </Grid>
                        </Border>
                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="True">

                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

效果是这样滴:

 

 

哈哈,成功了。再写一个textbox样式

代码如下 :

<Style TargetType="{x:Type TextBox}" x:Key="LineTextBox">
        <Setter Property="FontSize" Value="{StaticResource FontSizeLarge}"/>
        <Setter Property="Padding" Value="10"/>
        <Setter Property="Margin" Value="0 5 0 5"/>
        <Setter Property="BorderBrush" Value="#007Add"/>
        <Setter Property="BorderThickness" Value="0 0 0 1"/>
        <Setter Property="CaretBrush" Value="White"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Foreground" Value="White"/>

        <Setter Property="Template" >
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Grid>
                        <Border x:Name="border" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                SnapsToDevicePixels="True">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="False" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                        </Border>

                        <TextBlock IsHitTestVisible="False"
                                   Text="{TemplateBinding Tag}"
                                   x:Name="placeholder"
                                   FontFamily="{TemplateBinding FontFamily}"
                                   Padding="{TemplateBinding Padding}"
                                   VerticalAlignment="Center"
                                   HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                   Foreground="Gray"
                                   >
                            <TextBlock.Style>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Setter Property="Visibility" Value="Collapsed" />
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}" Value="">
                                            <Setter Property="Visibility" Value="Visible" />
                                        </DataTrigger>
                                    </Style.Triggers>
</Style>
                            </TextBlock.Style>

                        </TextBlock>

                    </Grid>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF7EB4EA"/>
                        </Trigger>
                        <Trigger Property="IsKeyboardFocused" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF569DE5"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

效果是这样滴:

 

 

下面就是在MainWindow.xaml中使用样式~

<TextBox Style="{StaticResource SearchTextBox}"/>
<TextBox Width="200" Style="{StaticResource LineTextBox}"
    Tag="请输入字符"/>

动态效果见文章顶部动图~

看到这里明白了吧,Tag就是水印~

标签:自定义,样式,xaml,搜索,WPF,代码,TextBox
From: https://www.cnblogs.com/cdaniu/p/16967934.html

相关文章

  • Android 动画实现 从基础到自定义
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • 调用自定义的包
    创建被调用的包mkdirtestcdtestgomodinitexample.com/testcattest/test.gopackagetestpackagetestimport"fmt"//func定义函数//Hello函数名//......
  • 自定义RBAC(3)
    您好,我是湘王,这是我的51CTO博客,欢迎您来,欢迎您再来~​RBAC类型的权限,本质上是一种对资源访问路径的控制,且具有典型的树型层次结构。而树型结构,天然地就有父结点和子结点的关......
  • WPF学习之X名称空间详解
    X名称空间里面的成员(如X:Name,X:Class)都是写给XAML编译器看的、用来引导XAML代码将XAML代码编译为CLR代码。4.1X名称空间里面到底都有些什么?x名称空间映射的是:http://sch......
  • 自定义RBAC(3)
    您好,我是湘王,这是我的博客园,欢迎您来,欢迎您再来~ RBAC类型的权限,本质上是一种对资源访问路径的控制,且具有典型的树型层次结构。而树型结构,天然地就有父结点和子结点的关......
  • vue实现自定义字体库
      先看效果是不是你所需要的,再看具体如何实现。   效果如下图所示:         有些字体需要下载,用图片就会变得很不清楚,这样我们就需要去下载字......
  • Android自定义View和Canvas绘图解析
    自定义view的流程分为measure、layout、draw三个主要步骤,今天我们通过源码来分下下measure的过程我们从顶级view开始,顶级view即DecorView,view的事件都是先经过这个DecorVi......
  • 使用.net 6.0框架的WPF应用如何引用System.Windows.Forms这个dll(转载)
      在.net6.0的WPF应用中,想使用OpenFileDialog这个类或者FolderBrowserDialog这个类,是无法找到System.Windows.Forms这个dll引用的,即使从系统C盘里搜索到System.Windo......
  • (16) WPF 导航控件
    一、 ​​Frame​​ 二、 ​​Hyperlink​​ 三、 ​​Page​​ 四、​​NavigationWindow​​ 五、​​TabControl​​......
  • (15) WPF 菜单控件
    一、 ​​ContextMenu​​ 二、 ​​Menu​​ 三、​​ToolBar​​......