WPF TextBox输入显示提示

本文介绍了如何在WPF中创建一个TextBox,使其在输入数字时具有放大显示和提示功能,类似于网上支付时的银行卡号输入体验。示例来源于网络分享。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在网上支付输入银行卡的时候,经常看到输入的数字会放大和提示。

下面是WPF版的一个例子。

Code public class ZoomTextTooltip : FrameworkElement
    {
        public object ZoomText
        {
            get { return (object)GetValue(ZoomTextProperty); }
            set { SetValue(ZoomTextProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ZoomText.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ZoomTextProperty =
            DependencyProperty.Register("ZoomText", typeof(object), typeof(ZoomTextTooltip), new UIPropertyMetadata(null,
                (o, args) =>
                {
                    var textBox = args.NewValue as TextBox;
                    if (textBox == null)
                        return;
                    var zoomTextTooltip = o as ZoomTextTooltip;
                    if (zoomTextTooltip == null)
                        return;
                    var popup = new Popup();
                    var textBlock = new TextBlock
                    {
                        Text = textBox.Text,
                        FontWeight = FontWeights.Bold,
                        FontSize = zoomTextTooltip.CustomFontSize,
                        Background = zoomTextTooltip.CustomBackground,
                        Foreground = zoomTextTooltip.CustomForeground
                    };
                    var binding = new Binding();
                    binding.Source = textBox;
                    binding.Path = new PropertyPath("IsKeyboardFocused");
                    BindingOperations.SetBinding(popup, Popup.StaysOpenProperty, binding);

                    var inputText = zoomTextTooltip.AddBlockString(textBox.Text.Trim(), zoomTextTooltip.BlockCount);
                    textBlock.Text = inputText;
                    popup.Child = textBlock;

                    textBox.GotFocus += (sender, eventArgs) =>
                                               {
                                                   popup.PlacementTarget = textBox;
                                                   popup.Placement = PlacementMode.Top;
                                                   popup.IsOpen = true;
                                               };
                    textBox.TextChanged += (sender, eventArgs) =>
                                               {
                                                   var addBlockString = zoomTextTooltip.AddBlockString(textBox.Text.Trim(), zoomTextTooltip.BlockCount);
                                                   textBlock.Text = addBlockString;
                                               };

                    textBox.LostFocus += (sender, eventArgs) =>
                                             {
                                                 popup.IsOpen = false;
                                             };
                }
                ));
        //字符串截取
        private string AddBlockString(string input, int count)
        {
            if (count == 0)
                return input;
            var blockinput = string.Empty;
            var length = Math.Ceiling((double)input.Length / count);
            for (int i = 0; i < length; i++)
            {
                var firstStart = i * count;
                var endString = input.Length - firstStart;
                if (endString < count)
                {
                    blockinput += input.Substring(firstStart);
                }
                else
                {
                    blockinput += input.Substring(firstStart, count);
                    blockinput += " ";
                }

            }
            return blockinput;
        }
        public double CustomFontSize
        {
            get { return (double)GetValue(CustomFontSizeProperty); }
            set { SetValue(CustomFontSizeProperty, value); }
        }

        public static readonly DependencyProperty CustomFontSizeProperty =
            DependencyProperty.Register("CustomFontSize", typeof(double), typeof(ZoomTextTooltip), new UIPropertyMetadata(12.0));

        public Brush CustomBackground
        {
            get { return (Brush)GetValue(CustomBackgroundProperty); }
            set { SetValue(CustomBackgroundProperty, value); }
        }

        public static readonly DependencyProperty CustomBackgroundProperty =
            DependencyProperty.Register("CustomBackground", typeof(Brush), typeof(ZoomTextTooltip), new UIPropertyMetadata(Brushes.White));

        public Brush CustomForeground
        {
            get { return (Brush)GetValue(CustomForegroundProperty); }
            set { SetValue(CustomForegroundProperty, value); }
        }

        public static readonly DependencyProperty CustomForegroundProperty =
            DependencyProperty.Register("CustomForeground", typeof(Brush), typeof(ZoomTextTooltip), new UIPropertyMetadata(Brushes.Black));

        public int BlockCount
        {
            get { return (int)GetValue(BlockCountProperty); }
            set { SetValue(BlockCountProperty, value); }
        }

        public static readonly DependencyProperty BlockCountProperty =
            DependencyProperty.Register("BlockCount", typeof(int), typeof(ZoomTextTooltip), new UIPropertyMetadata(0));



    }
XAML<TextBox  x:Name="tb" /> 
<ControlTest:ZoomTextTooltip CustomFontSize="20"  ZoomText="{Binding ElementName=tb}" BlockCount="4"/>

图示:

image

转载于:https://www.cnblogs.com/dingli/archive/2013/01/06/2847171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值