WPF日期选择器输入修改 [英] WPf Datepicker Input modification

查看:211
本文介绍了WPF日期选择器输入修改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用WPF / C#的形式。我期待通过编程修改/间preT用户打字输入端的 WPF工具包DatePicker的

I am creating a form using wpf/c#. I am looking to programatically change/interpret the user's typed input in the wpf toolkit DatePicker.

例如,在用户键入今天,而当控制失去焦点除preTED和设置使用我的C#函数的当前日期的日期。

For example, the user types "Today", and when the control looses focus the date is interpreted and set to the current date using my c# function.

我应该听LostFocus事件或有改变打字输入日期是如何解析的更好的办法?

Should I listen to the lostFocus event or is there a better way of changing how a typed input date is parsed?

我不小心更改日期选择器的显示格式。我使用MVVM模式开发这个应用程序。

I do not care to change the display format of the date picker. I am developing this application using the mvvm pattern.

推荐答案

好了,所以最后我看着的DatePicker 来源$ C ​​$ c和孤单并不多,我可以做转换器等方面,因为大多数的东西是私人的,仅有的两个可用的格式是短和长。最后,我将使用该解决方案通过以上阿维亚德创建自己的控制,可能在一部分。但是,这里有一个快速的临时解决方案,在此之前(其中 DateHelper 是我的自定义分析器类):

Ok so finally I looked into the sourcecode of DatePicker and theres not much I can do in terms of converters etc as most stuff is private and the only two available formats are "short" and "long". Finally, I will have to create my own control, probably in part using the solution above by Aviad. However, here's a quick temporary solution until then (where DateHelper is my custom parser class):

   public class CustomDatePicker : DatePicker
    {
        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.TryTransformDate();
            }

            base.OnPreviewKeyDown(e);
        }

        protected override void OnPreviewLostKeyboardFocus(KeyboardFocusChangedEventArgs e)
        {
            this.TryTransformDate();
            base.OnPreviewLostKeyboardFocus(e);
        }

        protected void TryTransformDate()
        {
            DateTime tryDate;
            if (DateHelper.TryParseDate(this.Text, out tryDate))
            {
                switch (this.SelectedDateFormat)
                {
                    case DatePickerFormat.Short: 
                        {
                            this.Text = tryDate.ToShortDateString();
                            break;
                        }

                    case DatePickerFormat.Long: 
                        {
                            this.Text = tryDate.ToLongDateString();
                            break;
                        }
                }
            }

        }
    }

这篇关于WPF日期选择器输入修改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆