This is a migrated thread and some comments may be shown as answers.

RadDateTimePicker sets time portion when bound to nullable DateTime even when InputMode="DatePicker"

6 Answers 102 Views
DateTimePicker
This is a migrated thread and some comments may be shown as answers.
Wellpartner
Top achievements
Rank 1
Wellpartner asked on 18 Nov 2010, 09:51 PM
The control is bound to a DateTime? and I've specified InputMode="DatePicker" and still it puts the current time into the date selected from the calendar.  How can I get just the date portion only?

6 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 23 Nov 2010, 03:36 PM
Hi,

There is no Date type in the WPF framework. There is only DateTime that represents the Date and the Time value. If you want to get only the Date part of the DateTime value, you could use DateTime.ToShortDateString() method. 

All the best,
George
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Wellpartner
Top achievements
Rank 1
answered on 23 Nov 2010, 04:48 PM
I understand there is no Date type.  Here is a sample app: when you select a date from the RadDateTimePicker control, the TextBlock control, which is bound to the same member, shows both Date + Time value, even though RadDateTimePicker.InputMode="DatePicker".

 <Window x:Class="BindingToNullableDateTime.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
        Title="Window1" Height="300" Width="300">
    <Grid Name="MainGrid" DataContext="{Binding}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <telerik:RadDateTimePicker Grid.Row="0"
                                   InputMode="DatePicker"
                                   Margin="5"
                                   SelectedValue="{Binding MyNullableDateTime, Mode=TwoWay}" />
        <TextBlock Grid.Row="1"
                   Margin="5"
                   Text="{Binding MyNullableDateTime}" />
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BindingToNullableDateTime
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        private DateTime? myNullableDateTime;
        public DateTime? MyNullableDateTime
        {
            get { return myNullableDateTime; }
            set
            {
                myNullableDateTime = value;
                Notify("MyNullableDateTime");
            }
        }

        public Window1()
        {
            InitializeComponent();
            MainGrid.DataContext = this;
        }
    }
}

0
George
Telerik team
answered on 24 Nov 2010, 05:22 PM
Hello Wellpartner,

I would suggest you to set a StringFormat to the binding:

<TextBlock Grid.Row="1"
                   Margin="5"
                   Text="{Binding MyNullableDateTime, StringFormat=dd-MM-yyyy}" />
 
I hope this suits your needs.


Kind regards,
George
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Wellpartner
Top achievements
Rank 1
answered on 24 Nov 2010, 08:00 PM
George,

The TextBlock control is not the issue.  The TextBlock control is simply so you can see what the MyNullableDateTime value is.  The issue is that RadDateTimePicker sets the MyNullableDateTime value with date + time.  When I save this value to the database I only want date + "12:00:00 AM".  I am handling this with code in the setter, but I'm wondering if there is a way to do this with XAML.

Thanks,
Sam
0
George
Telerik team
answered on 28 Nov 2010, 07:48 PM
Hello,

I will paste my answer from the support ticket here as well:

This issue is fixed in our latest Q3 binaries. Could you please try our Q3 release and let me know if you still have any problems with RadControls. I will be glad to assist you further. 

Greetings,
George
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Wellpartner
Top achievements
Rank 1
answered on 29 Nov 2010, 05:55 PM
Thanks, George - that solved it.
Tags
DateTimePicker
Asked by
Wellpartner
Top achievements
Rank 1
Answers by
George
Telerik team
Wellpartner
Top achievements
Rank 1
Share this question
or