DatePicker.SelectedDate 在输入文本时不会改变 [英] DatePicker.SelectedDate not changing when Text is input

查看:15
本文介绍了DatePicker.SelectedDate 在输入文本时不会改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的用户通过 DatePicker 中的 Calander 控件选择日期时,该值会正确绑定到基础对象.但是,如果用户在 DatePicker 中键入日期,然后单击按钮,则文本不会设置为 SelectedDate 属性.

When my users select a date via the Calander control within the DatePicker, the value gets correctly bound to the underlying object. BUT, if the user types the date within the DatePicker, then clicks a button, the text is not set to the SelectedDate property.

用户必须从 DatePicker 内的 TextBox 中移除光标才能更新绑定对象.

The user has to remove the cursor from the TextBox within the DatePicker for the bound object to be updated.

 <toolkit:DatePicker Name="_dpField" Grid.Column="1" MinWidth="100"
               ToolTip="{Binding Path=ToolTipText}"
               TextInput="_dpField_TextInput"
               SelectedDate="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>

帮助!我如何确保在按钮事件代码中使用了这个类型化的值?

HELP! how do i make sure that this typed value is used within the buttons event code?

谢谢!

推荐答案

您可以使用转换器将输入的文本解析为有效的日期时间

You can use a converter for parsing your typed text to a valid datetime

示例

 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string strValue = System.Convert.ToString(value);
        DateTime resultDateTime;
        if (DateTime.TryParse(strValue, out resultDateTime))
        {
            return resultDateTime;
        }
        return value;

    }

Xaml

     <Controls:DatePicker 
     Text="{Binding OrderDate,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
     SelectedDate="{Binding RelativeSource={RelativeSource Self},Path=Text,
     Converter={StaticResource DateConverter}}">

这篇关于DatePicker.SelectedDate 在输入文本时不会改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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