wpf DatePicker.SelectedDate 在输入文本时不会改变

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4542291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 22:27:09  来源:igfitidea点击:

DatePicker.SelectedDate not changing when Text is input

wpftextdatepickerwpftoolkit

提问by Nathan Tregillus

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 中的 Calander 控件选择日期时,该值会正确绑定到基础对象。但是,如果用户在 DatePicker 中键入日期,然后单击按钮,则文本不会设置为 SelectedDate 属性。

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

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

 <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?

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

Thanks!

谢谢!

采纳答案by biju

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

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

Sample

样本

 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

xml

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

回答by Slampen

I found a easier solution where I don't need the DateConverter.

我找到了一个更简单的解决方案,我不需要DateConverter.

I only bound to the TextProperty and use TargetNullValue=''.

我只绑定到TextProperty 并使用TargetNullValue=''.

<DatePicker x:Name = "dpDisbursementDate" 
            Text = "{Binding NameOfMyProperty, Mode=TwoWay,    
            UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, 
            TargetNullValue=''}"/>

回答by Abdalwhab Bakheet

The only solution that I could find is to disable entering of the date in the DatePicker by setting Focusable="False"and only allowing selection from the calendar. This way we can at least make sure that we get the correct date.

我能找到的唯一解决方案是通过设置Focusable="False"和仅允许从日历中进行选择来禁用在 DatePicker 中输入日期。这样我们至少可以确保我们得到正确的日期。

回答by user3771230

Here a simple solution, that helped me even with the european/german date format "dd.MM.yyyy".

这是一个简单的解决方案,即使使用欧洲/德国日期格式“dd.MM.yyyy”也对我有所帮助。

Add to your xaml root element

添加到您的 xaml 根元素

xml:lang="de-AT"

and the datepicker looks like this:

日期选择器看起来像这样:

<DatePicker SelectedDate="{Binding PropertyName, StringFormat=dd.MM.yyyy}" Name="datePicker" />

hope it works for you!

希望这对你有用!

回答by Rob

This is probably a bit late, but I've been stuck on this for a while now.

这可能有点晚了,但我已经坚持了一段时间了。

If you have another WPF element you can change focus to that at the beginning of your button press event, this will make the datepicker process any text entered in it's textbox. I've only tried this with a combobox but it seems to work and it allows you to still have custom formatting on your dates (ie 26/04/2016 rather than 04/26/2016). I assume you would be able to use an invisible element as well if you don't have anything to change focus to.

如果您有另一个 WPF 元素,您可以将焦点更改为按钮按下事件开始时的焦点,这将使日期选择器处理在其文本框中输入的任何文本。我只用组合框试过这个,但它似乎有效,它允许你在你的日期上仍然有自定义格式(即 26/04/2016 而不是 04/26/2016)。我假设如果您没有任何要改变焦点的东西,您也可以使用不可见元素。

    private void btnInbound_Complete_Click(object sender, RoutedEventArgs e)
    {
        if (Validation())
        {
            comboInbound_Result.Focus();//THIS IS SO THAT ANY MANUAL DATEPICKER ENTRY IS ACCEPTED BEFORE THE REST OF THE BUTTON CODE IS RUN
            SQLinbound_CompleteItem();
            ClearAll();
        }
    }