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

Time format Issue

3 Answers 55 Views
TimePicker
This is a migrated thread and some comments may be shown as answers.
Richard van Diggelen
Top achievements
Rank 1
Richard van Diggelen asked on 31 Mar 2011, 05:33 AM
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

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 05 Apr 2011, 06:41 PM
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
0
Tore
Top achievements
Rank 1
answered on 24 Mar 2012, 03:16 AM
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 

0
Konstantina
Telerik team
answered on 29 Mar 2012, 09:14 AM
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 >>
Tags
TimePicker
Asked by
Richard van Diggelen
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Tore
Top achievements
Rank 1
Konstantina
Telerik team
Share this question
or