• WPF自定义控件与样式(3)-TextBox & RichTextBox & PasswordBox样式、水印、Label标签、功能扩展...


    一.前言.预览

      申明WPF自定义控件与样式是一个系列文章,前后是有些关联的,但大多是按照由简到繁的顺序逐步发布的等,若有不明白的地方可以参考本系列前面的文章,文末附有部分文章链接。

    本文主要是对文本输入控件进行样式开发,及相关扩展功能开发,主要内容包括:

    • 基本文本框TextBox控件样式及扩展功能,实现了样式、水印、Label标签、功能扩展;
    • 富文本框RichTextBox控件样式;
    • 密码输入框PasswordBox控件样式及扩展功能;

    效果图:

    二.基本文本框TextBox控件样式及扩展功能

    2.1 TextBox基本样式

    样式代码如下:  

    1. <!--TextBox默认样式-->
    2. <Style TargetType="{x:Type TextBox}" x:Key="DefaultTextBox"> <Setter Property="ContextMenu" Value="{DynamicResource TextBoxContextMenu}" /> <Setter Property="SelectionBrush" Value="{StaticResource TextSelectionBrush}" /> <Setter Property="FontFamily" Value="{StaticResource FontFamily}" /> <Setter Property="FontSize" Value="{StaticResource FontSize}" /> <Setter Property="BorderThickness" Value="1" /> <Setter Property="MinHeight" Value="26" /> <Setter Property="Width" Value="100" /> <Setter Property="Background" Value="{StaticResource TextBackground}" /> <Setter Property="Foreground" Value="{StaticResource TextForeground}" /> <Setter Property="Padding" Value="0" /> <Setter Property="BorderBrush" Value="{StaticResource ControlBorderBrush}" /> <Setter Property="local:ControlAttachProperty.FocusBorderBrush" Value="{StaticResource FocusBorderBrush}" /> <Setter Property="local:ControlAttachProperty.MouseOverBorderBrush" Value="{StaticResource MouseOverBorderBrush}" /> <Setter Property="VerticalContentAlignment" Value="Center" /> <!-- change SnapsToDevicePixels to True to view a better border and validation error --> <Setter Property="SnapsToDevicePixels" Value="True" /> <!--英 ['kærət] 美 ['kærət] 插入符号--> <Setter Property="CaretBrush" Value="{StaticResource TextForeground}" /> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Grid x:Name="PART_Root"> <Border x:Name="Bg" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" CornerRadius="{TemplateBinding local:ControlAttachProperty.CornerRadius}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" /> <Grid x:Name="PART_InnerGrid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <!--Label区域--> <ContentControl x:Name="Label" Margin="1" Template="{TemplateBinding local:ControlAttachProperty.LabelTemplate}" Content="{TemplateBinding local:ControlAttachProperty.Label}"/> <!--内容区域--> <ScrollViewer x:Name="PART_ContentHost"
  • 相关阅读:
    没那么简单的单例模式
    Mac 中 vim 插件配置 —— 以YouCompleteMe 为例
    Python实现B站视频数据信息内容采集
    SpringBoot整合liquibase
    Python基础之数据库:5、创建表的完整语法、MySQL数据类型
    用了那么久的 Java For 循环,你知道哪种方式效率最高吗?
    软件测试 | 当面试时被问到“搭建过测试环境吗”, 身为小白要怎么回答?
    多款大模型向公众开放,百模大战再升级?
    Windows 下基于 MikTeX 的 Latex 环境配置小记
    PerfView专题 (第十一篇):使用 Diff 功能洞察 C# 内存泄漏增量
  • 原文地址:https://blog.csdn.net/LJianDong/article/details/127738499