Override OnPreviewKeyDown for DateTimeEdit
Hi
I'm trying to override the OnPreviewKeyDown event attached the to DateTimeEdit control so i can handle alpha shortcut keys.
I've been trying to do this using a controltemplate with the following code defined in a class without much luck.
public class DTPicker : DateTimeEdit
{
public DTPicker()
{
this.DefaultStyleKey = typeof(DTPicker);
}
protected override void OnPreviewKeyDown(KeyEventArgs e)
{
DateTime workingDt = (this.DateTime == null ? System.DateTime.Today : (DateTime)this.DateTime);
if (e.Key == Key.Delete)
{
this.DateTime = null;
e.Handled = true;
}
else if (e.Key == Key.T)
{
this.DateTime = System.DateTime.Today;
e.Handled = true;
}
else if (e.Key == Key.OemPlus || e.Key == Key.Add)
this.DateTime = workingDt.AddDays(1);
else if (e.Key == Key.OemMinus || e.Key == Key.Subtract)
this.DateTime = workingDt.AddDays(-1);
}
else
base.OnPreviewKeyDown(e);
}
}
and using a style defined in the global resource dictionary pointing to the class containing the above code
<Style TargetType="sf:DateTimeEdit">
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ct:DTPicker}" />
</Setter.Value>
</Setter>
</Style>
Unfortunately receiving a template error with this so hoping you could advise what I need to change to get this working.
Thanks
Allan
SIGN IN To post a reply.
3 Replies
1 reply marked as answer
NI
Ninja
Syncfusion Team
August 7, 2020 09:50 AM UTC
Hi Allan,
Thanks for contacting Syncfusion support.
We have checked your query and it seems that you have created DTPicker to override the OnPreviewKeyDown event of DateTimeEdit and in styles you have tried to apply DTPicker as control template for controls of type DateTimeEdit in ResourceDictionary. It is not possible to apply the DTPicker as a control template. The only solution is to use DTPicker instead of DateTimeEdit as control, especially not in styles and it will allow you to meet your requirement.
Regards,
Niranjan Kumar Gopalan
Marked as answer
AL
Allan
August 7, 2020 10:18 AM UTC
Thanks Niranjan
That works perfectly.
Cheers
Allan.
NI
Ninja
Syncfusion Team
August 7, 2020 11:13 AM UTC
Hi Allan,
Thanks for your update.
We are glad to know reported query has been resolved at your end. Have a safe day!
Regards,
Niranjan Kumar Gopalan
SIGN IN To post a reply.