问题
Requirement: Allow user to enter date in the format of ###### while also have the ability to choose it from a DatePicker (assuming all years entered will be Post 2000.) So for example, user enters 050513 it becomes 5/5/2013. Or if they choose it from the DatePicker, provide the same display. They also want it to select all when the control receives focus so they can easily overwrite any pre-existing date.
What I tried: My xaml is much stronger than my c# so I tried to get creative and got really darn close but no cigar, so hoping someone could fill in the blanks but here's where I got. I figured I could leverage what I know about RadMaskedTextBox and RadDateTimePicker so I intertwined their properties a bit (with the idea I could just use the Box for entry, and the picker for just the icon) but for testing purposes it was something like this;
<StackPanel Grid.Column="2" Orientation="Horizontal" HorizontalAlignment="Center" >
<telerik:RadMaskedTextBox x:Name="radMTB"
Value="{Binding SelectedDate, Mode=TwoWay,
ElementName=radDP}"
MaskType="DateTime"
Mask="d" SelectionOnFocus="SelectAll" />
<telerik:RadDateTimePicker x:Name="radDP"
InputMode="DatePicker"
SelectedDate="{Binding SelectedDate, Mode=TwoWay, ElementName=radMTB}"/>
</StackPanel>
This works great except for the fact that it still requires them to type the whole date with slashes. So I changed the MaskType to Standard with a Mask="##/##/\2\0##" to insert the "20" so they could enter like 121212 and it become 12/12/2012 right? Thought I had it until I tried to pass the value off to the DatePicker. I Bound the SelectedDate on the Picker to TextWithLiterals to get the display value, no luck. Tried Display.Text, Display.Value, just Text, just Value. It wouldn't work with any of them to bind too and pass off to the DatePicker to work...
So I guess my question is, can I still bind to that literal display somehow and make this work using only xaml? Or is ParseDateTimeValue code inevitably required for the RadDateTimePicker? If so, could someone please share some knowledge and help a guy out with it? Or is there another way all together to fulfill the sought after requirements? This cant be the first time this has come up?
Thanks for reading either way and cheers!
回答1:
Instead of using RadMaskedTextBox, trying using Telerik's RadMaskedDateTimeInput. It is quite a bit more flexible with DateTime values.
<telerik:RadMaskedDateTimeInput x:Name="radMTB1"
IsClearButtonVisible="False"
FormatString="{}{0:M/d/yyyy}"
Mask="MMddyy"
SelectionOnFocus="SelectAll"
Value="{Binding SelectedDate, Mode=TwoWay, ElementName=radDP2}" />
More info: http://www.telerik.com/help/silverlight/radmaskedinput-features-controls-datetime.html
来源:https://stackoverflow.com/questions/13574512/raddatetimepicker-mask-for-data-entry