当前位置: 首页 > news >正文

WPF 自定义TextBox带水印控件,可设置圆角

一、简单设置水印TextBox控件,废话不多说看代码:

  <TextBox TextWrapping="Wrap" Margin="10" Height="69"  Visibility="Visible">
                <TextBox.Style>
                    <Style TargetType="TextBox">
                        <Style.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsFocused" Value="false"/>
                                    <Condition Property="Text" Value=""/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background">
                                    <Setter.Value>
                                        <VisualBrush AlignmentX="Left" AlignmentY="Top" Stretch="None">
                                            <VisualBrush.Visual>
                                                <TextBlock Padding="5 2" Background="Transparent" TextWrapping="Wrap" Height="40" Foreground="Silver">您的评价对网友有很重要的参考作用,请认真填<LineBreak/>写,谢谢合作!</TextBlock>
                                            </VisualBrush.Visual>
                                        </VisualBrush>
                                  </Setter.Value>
                              </Setter>
                         </MultiTrigger>
                    </Style.Triggers>
               </Style>
      </TextBox.Style>
</TextBox>

这里的style可以单独提出来,方便多处使用。

设置之后的效果如下:

二、扩展增强TextBox

有的时候会碰到TextBox里面需要添加按钮,或者TextBox需要设置圆角。这个时候就需要添加自定义控件,添加依赖属性来扩展功能了。

2.1 添加自定义控件TextBoxEx

代码如下:

 public class TextBoxEx : TextBox
    {
        static TextBoxEx()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TextBoxEx), new FrameworkPropertyMetadata(typeof(TextBoxEx)));
        }

        public TextBoxEx()
        {
            CommandBindings.Add(new CommandBinding(SearchClickCommand, delegate
            {
                SearchClick?.Invoke(this, null);
            }));
        }

        public static RoutedUICommand SearchClickCommand = new RoutedUICommand(nameof(SearchClickCommand), nameof(SearchClickCommand), typeof(RoutedUICommand));

        public event EventHandler SearchClick;
        public string WaterMark
        {
            get { return (string)GetValue(WaterMarkProperty); }
            set { SetValue(WaterMarkProperty, value); }
        }

        public static readonly DependencyProperty WaterMarkProperty =
            DependencyProperty.Register("WaterMark", typeof(string), typeof(TextBoxEx), new PropertyMetadata(null));


        public ImageSource Icon
        {
            get { return (ImageSource)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty IconProperty =
            DependencyProperty.Register("Icon", typeof(ImageSource), typeof(TextBoxEx), new PropertyMetadata(null));


        public new Brush BorderBrush
        {
            get { return (Brush)GetValue(BorderBrushProperty); }
            set { SetValue(BorderBrushProperty, value); }
        }

        public static readonly new DependencyProperty BorderBrushProperty =
            DependencyProperty.Register("BorderBrush", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(234,234,234))));


        public Brush MouseOverBorderBrush
        {
            get { return (Brush)GetValue(MouseOverBorderBrushProperty); }
            set { SetValue(MouseOverBorderBrushProperty, value); }
        }

        public static readonly DependencyProperty MouseOverBorderBrushProperty =
            DependencyProperty.Register("MouseOverBorderBrush", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(79,173,216))));



        public new Brush Background
        {
            get { return (Brush)GetValue(BackgroundProperty); }
            set { SetValue(BackgroundProperty, value); }
        }

        public static readonly new DependencyProperty BackgroundProperty =
            DependencyProperty.Register("Background", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(106,103,90))));


        public Brush MouseOverBackground
        {
            get { return (Brush)GetValue(MouseOverBackgroundProperty); }
            set { SetValue(MouseOverBackgroundProperty, value); }
        }

        public static readonly DependencyProperty MouseOverBackgroundProperty =
            DependencyProperty.Register("MouseOverBackground", typeof(Brush), typeof(TextBoxEx), new PropertyMetadata(Color.FromRgb(106,103,90)));


        public CornerRadius CornerRadius
        {
            get { return (CornerRadius)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public static readonly DependencyProperty CornerRadiusProperty =
            DependencyProperty.Register("CornerRadius", typeof(CornerRadius), typeof(TextBoxEx), new PropertyMetadata(new CornerRadius(0)));



        public IconDirection IconDirection
        {
            get { return (IconDirection)GetValue(IconDirectionProperty); }
            set { SetValue(IconDirectionProperty, value); }
        }

        public static readonly DependencyProperty IconDirectionProperty =
            DependencyProperty.Register("IconDirection", typeof(IconDirection), typeof(TextBoxEx), new PropertyMetadata(IconDirection.Right));


    }

    public enum IconDirection
    {
        Left,
        Right
    }

 

2.2 设置TextBoxEx样式

 <Style TargetType="{x:Type local:TextBoxEx}">
        <Setter Property="Foreground" Value="#555558"/>
        <Setter Property="Background" Value="#6a675a"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TextBoxEx}">
                    <Border x:Name="_border" CornerRadius="{TemplateBinding CornerRadius}" Padding="{TemplateBinding CornerRadius,Converter={StaticResource cornerRadiusToThickness} }" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition x:Name="columleft" Width="auto"/>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition x:Name="columright" Width="auto"/>
                            </Grid.ColumnDefinitions>
                            <Image Source="{TemplateBinding Icon}"  Cursor="Hand" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseLeftButtonUp">
                                        <i:InvokeCommandAction Command="local:TextBoxEx.SearchClickCommand"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>
                            <ScrollViewer x:Name="PART_ContentHost" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Grid.Column="1"/>
                            <TextBlock x:Name="_txtWater" IsHitTestVisible="False" Margin="3 0 0 0" VerticalAlignment="Center" Foreground="#9f9f9f"  Text="{TemplateBinding WaterMark}" Visibility="Hidden" Grid.Column="1"/>
                            <Image Source="{TemplateBinding Icon}" Cursor="Hand" Stretch="None" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center">
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="MouseLeftButtonUp">
                                        <i:InvokeCommandAction Command="local:TextBoxEx.SearchClickCommand"/>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Image>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value="">
                            <Setter TargetName="_txtWater" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IconDirection}" Value="Left">
                            <Setter TargetName="columright" Property="Width" Value="0" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IconDirection}" Value="Right">
                            <Setter TargetName="columleft" Property="Width" Value="0" />
                        </DataTrigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="{Binding MouseOverBorderBrush,RelativeSource={RelativeSource TemplatedParent}}"/>
                            <Setter Property="Background" Value="{Binding MouseOverBackground,RelativeSource={RelativeSource TemplatedParent}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

这里面使用了一个转换器cornerRadiusToThickness,转换器的作用是当TextBox设置了圆角之后,保证TextBox里的内容不溢出圆角。WPF转换器的使用大家可以百度。

转换器的内容如下:

  public class CornerRadiusToThickness : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Thickness result = new Thickness(0);
            if (value is CornerRadius)
            {
                var tem = (CornerRadius)value;
                result = new Thickness(tem.TopLeft, 0, tem.TopRight, 0);
            }
            return result;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

另外为了触发TextBox按钮的单击事件,添加了这个两个dll文件引用,Microsoft.Expression.Interactions.dll和System.Windows.Interactivity.dll。

在顶部定义 xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

2.3 引用方法

 <local:TextBoxEx WaterMark="请输入搜索内容..." SearchClick="TextBoxEx_SearchClick" KeyDown="TextBoxEx_KeyDown" Background="#dfe1e3" VerticalAlignment="Center" MouseOverBackground="#dfe1e3" CornerRadius="13.5" IconDirection="Right" Icon="/Images/Title/search.png" Height="25" />

 

转载于:https://www.cnblogs.com/xiaomingg/p/8719577.html

相关文章:

  • SnapKit 最佳实践
  • Linux的磁盘配额
  • 【干货分享】SpringCloud微服务架构分布式组件如何共享session对象
  • 100. bootstrap 弹出对话框bootbox.confirm
  • jetty的使用
  • mysql架构
  • 详解node.js中的可读流(Readable)和可写流(Writeable)
  • 一文看懂JeffDean等提出的ENAS到底好在哪?
  • MXNet 作者李沐:用深度学习做图像分类,教程+代码
  • Map集合、散列表、红黑树介绍
  • centos7.4系统的虚拟机网络配置教程
  • win10 php安装redis 扩展
  • 6、通过Appium Desktop 实现录制功能
  • 文件上传漏洞攻击
  • Micropython TPYBoard V10X拼插编程实践之定时器 代码不精通?...
  • 2017年终总结、随想
  • angular学习第一篇-----环境搭建
  • bearychat的java client
  • CSS中外联样式表代表的含义
  • ES学习笔记(12)--Symbol
  • go append函数以及写入
  • java8-模拟hadoop
  • pdf文件如何在线转换为jpg图片
  • Python学习之路13-记分
  • react-native 安卓真机环境搭建
  • windows-nginx-https-本地配置
  • 基于webpack 的 vue 多页架构
  • 理解 C# 泛型接口中的协变与逆变(抗变)
  • 收藏好这篇,别再只说“数据劫持”了
  • 通过获取异步加载JS文件进度实现一个canvas环形loading图
  • 小程序开发中的那些坑
  • 再谈express与koa的对比
  • PostgreSQL 快速给指定表每个字段创建索引 - 1
  • ​Python 3 新特性:类型注解
  • ​决定德拉瓦州地区版图的关键历史事件
  • ​如何在iOS手机上查看应用日志
  • #我与Java虚拟机的故事#连载16:打开Java世界大门的钥匙
  • (1)STL算法之遍历容器
  • (2022 CVPR) Unbiased Teacher v2
  • (libusb) usb口自动刷新
  • (补)B+树一些思想
  • (二)构建dubbo分布式平台-平台功能导图
  • (蓝桥杯每日一题)love
  • (六)库存超卖案例实战——使用mysql分布式锁解决“超卖”问题
  • (数据结构)顺序表的定义
  • (顺序)容器的好伴侣 --- 容器适配器
  • (四)Android布局类型(线性布局LinearLayout)
  • .CSS-hover 的解释
  • .NET 编写一个可以异步等待循环中任何一个部分的 Awaiter
  • .NET 表达式计算:Expression Evaluator
  • .NET 中 GetProcess 相关方法的性能
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本
  • .net反编译工具
  • .NET教程 - 字符串 编码 正则表达式(String Encoding Regular Express)
  • //解决validator验证插件多个name相同只验证第一的问题