I'm working on my first WPF app. In this case, using VS 2010. My users are used to typing the date like this: "09082010" (without the double quotes; this would represent today). After they enter that, then it gets converted to 9/8/2010. I've put the DatePicker control onto the WPF page, but if the user enters 09082010, then it doesn't recognize it as a date and ignores it. I've applied a IValueConverter, to no effect, again because it doesn't recognize "09082010" as a date. So, I'm wondering, is it possible to apply a mask to the textbox of the DatePicker in VS 2010, so that when a user enters 09082010 it will change that to 09/08/2010 (at least)?
+1
A:
Here's something you could probably do: handle the TextBox.TextChanged event in the DatePicker, then in the event handler, put your custom logic to parse the current text. Something like this:
<DatePicker x:Name="dp" TextBoxBase.TextChanged="DatePicker_TextChanged"/>
private void DatePicker_TextChanged(object sender, TextChangedEventArgs e)
{
DateTime dt;
DatePicker dp = (sender as DatePicker);
string currentText = (e.OriginalSource as TextBox).Text;
if (!DateTime.TryParse(currentText, out dt))
{
try
{
string month = currentText.Substring(0,2);
string day = currentText.Substring(2,2);
string year = currentText.Substring(4,4);
dt = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day));
dp.SelectedDate = dt;
}
catch (Exception ex)
{
dp.SelectedDate = null;
}
}
}
I know it ain't pretty. But this could be a start.
karmicpuppet
2010-09-08 19:07:29
Hmmm. I'll give it a try. This assumes that the user will have entered a date in exactly 8 characters, rather than something like "9082010". If this works, then it would become a training issue. I'll let you know how it goes. Thank you, karmicpuppet.
Rod
2010-09-08 20:06:49
OK, I just gave it a go. Unfortunately, there is no TextChanged event for the DatePicker control. At least it doesn't look that way to me. So I put it into the SelectedDateChanged event, then I tested it by entering 09082010 into the text box of the DatePicker control, and set a breakpoint in the code to see if what happened. It never even fired off the SelectedDateChanged event.
Rod
2010-09-08 20:18:41
Yeah, I agree. In the real deal, it will certainly require a far more intelligent parsing logic than what I provided. But like I said, "this could be a start". I guess the main idea to get from this is that you can handle the TextChanged event from the DatePicker and provide your own parsing logic to set the DatePicker.SelectedDate property behind the scenes.
karmicpuppet
2010-09-08 20:19:37
You have to explicitly specify "TextBoxBase.TextChanged" event in the DatePicker, exactly as I wrote in my example
karmicpuppet
2010-09-08 20:20:51
Oh, I'm sorry, I missed the "TextBoxBase.TextChanged" event you mentioned in your code example.
Rod
2010-09-20 20:03:27
Wow, this was somewhat of a late reply. Been on vacation? =) Anyway, thanks for marking it correct.
karmicpuppet
2010-09-20 20:11:51