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

7 posts, 0 answers
  1. Wellpartner
    Wellpartner avatar
    15 posts
    Member since:
    Aug 2010

    Posted 18 Nov 2010 Link to this post

    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?
  2. George
    Admin
    George avatar
    1332 posts

    Posted 23 Nov 2010 Link to this post

    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
  3. UI for WPF is Visual Studio 2017 Ready
  4. Wellpartner
    Wellpartner avatar
    15 posts
    Member since:
    Aug 2010

    Posted 23 Nov 2010 Link to this post

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

  5. George
    Admin
    George avatar
    1332 posts

    Posted 24 Nov 2010 Link to this post

    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
  6. Wellpartner
    Wellpartner avatar
    15 posts
    Member since:
    Aug 2010

    Posted 24 Nov 2010 Link to this post

    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
  7. George
    Admin
    George avatar
    1332 posts

    Posted 28 Nov 2010 Link to this post

    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
  8. Wellpartner
    Wellpartner avatar
    15 posts
    Member since:
    Aug 2010

    Posted 29 Nov 2010 Link to this post

    Thanks, George - that solved it.
Back to Top
UI for WPF is Visual Studio 2017 Ready