使用依赖附加属性的WPF Masked TextBox

时间:2015-11-17 09:42:52

标签: c# wpf textbox datepicker

您能否建议我如何为DatePicker(datepickerTextBox)或TextBox进行屏蔽?该掩码格式应该是日期模式(取决于语言环境)。该行为使事件完成不完整的日期并将2位数年份转换为4位数年份。

    public static void uiControl_PreviewTextInput(object sender,TextCompositionEventArgs e)
    {

        MaskControl txt = (MaskControl)sender;
        e.Handled = true;

        int position = txt.SelectionStart;

        int seperatorPosition = maskProvider.ToDisplayString().IndexOf('/');

        if (position < txt.Text.Length)
        {
            position = GetNextCharacterPosition(position);
            if ((Keyboard.IsKeyToggled(Key.Insert) && maskProvider.Replace(e.Text, position)) ||
                     maskProvider.InsertAt(e.Text, position))
            {
                position++;
            }
            //position = GetNextCharacterPosition(position);
            txt.Text = maskProvider.ToString();

            if (position == seperatorPosition)
            {
                position = seperatorPosition++;
            }
        }
    }

0 个答案:

没有答案