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 get开发者_StackOverflows 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)?
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.
精彩评论