Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > TimePicker > Time format Issue

Not answered Time format Issue

Feed from this thread
  • Richard van Diggelen avatar

    Posted on Mar 31, 2011 (permalink)

    Hi,

    I had found an issue with WPF TimePicker control for .Net 4.0.
    When I type in 130 in the time field, it gets converted to 1PM. Its coz it considers it as 13 and 0.So it becomes 1:00PM instead of 1:30AM.
    Similary 230 converts to 11PM, thinking its 23:00hrs.
    This issue works fine in ASP.Net control (ie:130 gets converted to 1:30AM). I had tried with the demo version of the latest release.

    Is this a bug?

    Thanks

    Reply

  • Kaloyan Kaloyan admin's avatar

    Posted on Apr 5, 2011 (permalink)

    Hello Richard van Diggelen,

    This is the default parser behavior and it is not possible for a customization. A possible solution is to use the ParseDateTime event, where you can specify your own result. Let us know if you need any further information.


    All the best,
    Kaloyan
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

    Reply

  • Tore avatar

    Posted on Mar 23, 2012 (permalink)

    I have found a way to fix the RadTimePicker using WPF styling and event setters. First off, add a ResourceDictionary (ControlStyles.xaml): 

    <ResourceDictionary x:Class="EventSetterWPF.ControlStyles"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:tc="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input">


    <Style TargetType="{x:Type tc:RadTimePicker}">
    <EventSetter Event="ParseDateTimeValue" Handler="RadTimePicker_ParseDateTimeValue" />
    </Style>

    </ResourceDictionary>

    Next off, add a codebehind for the ResourceDictionary (ControlStyles.xaml.cs)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using Telerik.Windows.Controls;

    namespace EventSetterWPF
    {


    public partial class ControlStyles : ResourceDictionary
    {

    public ControlStyles()
    {
    InitializeComponent();
    }

    public void RadTimePicker_ParseDateTimeValue(object sender, ParseDateTimeEventArgs e)
    {
    if (e.IsParsingSuccessful)
    {
    string originalString = e.TextToParse.Replace(":", "");
    if (!string.IsNullOrEmpty(originalString) && originalString.Length == 4 && originalString.StartsWith("0"))
    {
    int enteredHour = -1;
    int enteredMinutes = -1;
    if (int.TryParse(originalString.Substring(0, 2), out enteredHour) &&
    (int.TryParse(originalString.Substring(2, 2), out enteredMinutes)) &&
    e.Result.HasValue)
    {
    DateTime originalValue = e.Result.Value;
    DateTime? newValue = new DateTime(originalValue.Year, originalValue.Month, originalValue.Day,
    enteredHour, enteredMinutes, 0);
    e.Result = newValue;
    }
    }
    }
    }

    }

    }

    Add the resource dictionary to App.xaml: 

    <Application x:Class="EventSetterWPF.App"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 StartupUri="TestRadTimePicker.xaml">
        <Application.Resources>
            <ResourceDictionary Source="ControlStyles.xaml" /> 
        </Application.Resources>
    </Application>
    

    Now, all your RadTimePicker will have this custom datetime parsing. Similar strategy can be used for RadDatePicker and RadDateTimePicker. I had to fix these issues at work today for multiple Telerik Controls. 



    For example, typing "0130" did not parse to 01:30 AM, rather it resulted in 13:00 (PM) being set. Using the strategy above made the parsing of the RadTimePicker work again. 

    I included a blog article for the interested reader here:  

    http://toreaurstad.blogspot.com/2012/03/styling-user-controls-in-wpf-with.html 

    Reply

  • Konstantina Konstantina admin's avatar

    Posted on Mar 29, 2012 (permalink)

    Hello Tore,

    Thank you for sharing your solution with us. It would be very helpful to all encountered the same problem.

    All the best,
    Konstantina
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>

    Reply

Back to Top

Skip Navigation LinksHome / Community & Support / Developer Productivity Tools Forums / WPF > TimePicker > Time format Issue
Related resources for "Time format Issue"

WPF TimePicker Features  |  Documentation  |  Demos  |  Telerik TV  |  Self-Paced Trainer  ]