【问题标题】:Insert hyphen automatically after every 4 characters in a TextBox在文本框中每 4 个字符后自动插入连字符
【发布时间】:2013-05-01 07:09:18
【问题描述】:

我想实现一些事情,当用户输入 4 个字符时,应在文本中添加一个连字符,然后用户再次输入 4 个字符,然后再次在 WPF 中自动添加连字符。

注意

“我想在用户在文本框中输入时(而不是在文本框失去焦点之后)实现这种行为,因为后者很容易实现”

使用MVVM模型,所以Dialog后面的代码应该是空的

【问题讨论】:

  • Daniel Hilgarth,实际上我只想使用普通的 wpf 文本框
  • 这是什么原因?
  • 我的前辈要求我这样做,即使我使用 MaskTextBox,我必须为连字符模式分配什么掩码
  • “使用MVVM模型,所以Dialog后面的代码应该是空的”这句话是错误的。

标签: wpf wpf-controls wpfdatagrid wpftoolkit wpf-4.0


【解决方案1】:

您可以使用视图模型中的属性来执行此操作,如下所示。但是您会发现文本框插入符中的光标不会移动到最后一个索引,并且由于插入符索引不可绑定,您需要创建一个附加属性以绑定到索引。

        private string _serial;

        public string Serial
        {
            get { return _serial; }
            set
            {
                if (_serial != value)
                {
                    _serial = value;
                    int res = 0;
                    Math.DivRem(_serial.Length, 4, out res);
                    if (res == 0)
                        Serial = string.Format("{0}-", _serial);
                    CaretIndex = _serial.Lenght - 1;
                    RaisePropertyChanged("Serial");
                }
            }
        }

这是您需要的 xaml 代码。

<Window x:Name="root" x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="WpfApplication1"
        Title="MainWindow" Height="350" Width="525">
    <TextBox
        Text="{Binding ElementName=root, Path=Serial, UpdateSourceTrigger=PropertyChanged}"
        local:TextIndexBinder.Index="{Binding ElementName=root, Path=Index}"
        />
</Window>

【讨论】:

  • 当我从不想要的文本框中失去焦点时,将调用属性的 setter 部分。我想要用户按键时的效果
  • 请注意绑定中的 UpdateSourceTrigger。它会立即更新。
【解决方案2】:

属性定义: 我们应该计算字符串中不包括连字符的字符 此处无法设置插入符号索引,因为尚未调用 OnPropertyChanged,因此 TextBox.Text 仍包含旧值,并且您无法设置大于文本长度的值:

 private string _serial;
 public string Serial
 {
     get { return _serial; }
     set
     {
         if (_serial != value)
         {
             _serial = value;
             int res = 0;
             int hyphensCount = _serial.Count(c => c.Equals('-'));
             Math.DivRem(_serial.Length - hyphensCount, 4, out res);
             if (res == 0)
                 _serial = string.Format("{0}-", _serial);
             OnPropertyChanged("Serial");
         }
     }
 }

行为 - 注册TextChanged 事件并将插入符号移到文本末尾:

 public class MoveCaretToEndBehavior: Behavior<TextBox>
{
    protected override void OnAttached()
    {
        AssociatedObject.TextChanged += new TextChangedEventHandler(AssociatedObject_TextChanged);
    }

    void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
    {
        AssociatedObject.CaretIndex = AssociatedObject.Text.Length;
    }
}

文本框 + 行为

<TextBox Text="{Binding Serial,UpdateSourceTrigger=PropertyChanged}">
        <i:Interaction.Behaviors>
            <local:MoveCaretToEndBehavior />
        </i:Interaction.Behaviors>


</TextBox>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-28
    • 1970-01-01
    • 2014-12-30
    • 2011-03-16
    • 2017-08-24
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    相关资源
    最近更新 更多