DatePicker.SelectedDate not changing when Text is input

冷暖自知 提交于 2019-12-18 15:05:24

问题


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.

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?

Thanks!


回答1:


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

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



回答2:


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

I only bound to the Text Property and use TargetNullValue=''.

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



回答3:


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.




回答4:


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

Add to your xaml root element

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!




回答5:


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.

    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();
        }
    }


来源:https://stackoverflow.com/questions/4542291/datepicker-selecteddate-not-changing-when-text-is-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!