Skip to main content
Home Forums General Silverlight New Features in Silverlight 3 Datepicker problem or bug?
16 replies. Latest Post by andrewaggb on August 28, 2009.
(0)
wackyphill
Member
37 points
119 Posts
06-20-2009 11:40 AM |
I have a DatePicker in a datagrid and when I type a date into it and tab into another cell the date does not save/bind. It reverts back to what it was before. It does work though if I type a new date, hit enter, and then go to a new cell but that should not be necessary.
Am I doing something wrong or is this a bug?
Here's the xaml:
basak
174 points
32 Posts
06-22-2009 5:39 PM |
Hi,
This is not a bug but the intended behaviour. SelectedDate is not set until you hit enter or open the Calendar pop up.
Thanks.
06-22-2009 6:14 PM |
Really? Silverlight 2 doesn't work that way. Why change the expected behavior in Silverlight 3?
Plus that's really unintuitive for a user. I have a hard time imagining anyone would want that behavior. When you type into a textbox you expect what you entered to remain unless what you typed was invalid. I can't think of a UI I've used in the past that requires the Enter key be pressed.
06-24-2009 1:45 PM |
It was the same behavior in Silverlight 2. SelectedDate was only committed when Enter key is hit. Can you please post all your project?
06-24-2009 10:36 PM |
OK.....Well I'm not sure if you've actually ever used Silverlight 2 before or not but you don't seem to know what you're talking about.
Here's a link to a very basic Silverlight 2 program written in 5 minutes to demonstrate that in Silverlight 2 if you place a DatePicker in a Datagrid you can type a date in to it, tab to a new cell, and your date will remain.
http://www.sendspace.com/file/7fqli7
I'm hoping someone out there who might be in a position to let the SL3 devs know, would please do so. This seems to be broken in SL3.
06-25-2009 1:54 PM |
I am a part of the dev team and I was talking about the DatePicker behaviour outside of a DataGrid since nothing has changed in that area in DatePicker since SL2. But the behaviour of DataGrid has changed. I let the teams know about the issue.
06-25-2009 6:57 PM |
Thank you.
yifung
Contributor
3313 points
540 Posts
06-25-2009 9:30 PM |
Basak is correct. The DataGrid changed to use explicty binding for editing for Silverlight 3, and this looks like an unfortunate side effect of that change. As a workaround, please use the following DateColumn implementation. I know this isn't ideal, and even the implementation of the DateColumn below isn't ideal, but I believe it's the best choice at this time.
The usage of the DateColumn is simple. It's similar to using the TextColumn. Of course in your case, you could modify it slightly so that it has a readonly Binding and an editing Binding
<data:DataGrid x:Name="dataGrid"> <data:DataGrid.Columns> <local:DataGridDateColumn Binding="{Binding Date}" /> </data:DataGrid.Columns> </data:DataGrid>
public class DataGridDateColumn : DataGridBoundColumn { private DatePicker _datePicker; private TextBox _textBox; protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { if (_datePicker == null) { _datePicker = new DatePicker(); _datePicker.SetBinding(DatePicker.SelectedDateProperty, this.Binding); } return _datePicker; } protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { TextBlock textBlock = new TextBlock(); textBlock.Margin = new Thickness(6); textBlock.VerticalAlignment = VerticalAlignment.Center; textBlock.SetBinding(TextBlock.TextProperty, this.Binding); return textBlock; } protected override object PrepareCellForEdit(FrameworkElement editingElement, RoutedEventArgs editingEventArgs) { if (_textBox == null) { SetUpTextBox(_datePicker); } return _datePicker.SelectedDate; } private void SetUpTextBox(DependencyObject parent) { _textBox = parent as TextBox; if (_textBox == null && parent != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++) { SetUpTextBox(VisualTreeHelper.GetChild(parent, i)); if (_textBox != null) { break; } } } if (_textBox != null) { _textBox.TextChanged += TextBox_TextChanged; } } private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { string dateText = _textBox.Text; DateTime date; if (DateTime.TryParse(dateText, out date)) { _textBox.TextChanged -= TextBox_TextChanged; int selectionStart = _textBox.SelectionStart; int selectionLength = _textBox.SelectionLength; _datePicker.SelectedDate = date; this.Dispatcher.BeginInvoke(delegate { _textBox.Text = dateText; _textBox.SelectionStart = selectionStart; _textBox.SelectionLength = selectionLength; _textBox.TextChanged += TextBox_TextChanged; }); } } }
avbersSL
164 points
78 Posts
06-26-2009 3:26 AM |
I'm hitting this problem this week also, and it is very unfortunate..
This 'workaround' is in my opinion a dirty hack ;) so I hope the SL team is able to fix it in the final SL 3... (which will be very very final if the july 10th release date still stands..)
07-11-2009 5:14 PM |
Unfortunately this problem still exists in SL3 RTW, but thank you yifung for the example. I had not seen a DataGridBoundColumn implementation before. I learned something.
07-13-2009 2:01 PM |
Thanks, I'm sorry about the inconvenience. This is definitely one of the worst issues in the SL 3 DataGrid.
07-16-2009 11:29 AM |
I was wondering what the SL team had to say about this issue....
Brian Br...
368 points
66 Posts
07-22-2009 3:40 PM |
Another possible workaround is to hook up to the DataGrid's CellEditEnding event and finalize the DatePicker's value. The DatePicker doesn't typically finalize its value until LostFocus, but this will cause it to do so synchronously.
void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { DatePicker datePicker = e.EditingElement as DatePicker; if (datePicker != null) { Queue children = new Queue(); children.Enqueue(datePicker); while (children.Count > 0) { DependencyObject child = children.Dequeue(); TextBox textBox = child as TextBox; if (textBox != null) { datePicker.Text = textBox.Text; return; } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(child); i++) { children.Enqueue(VisualTreeHelper.GetChild(child, i)); } } } }
estern
422 points
126 Posts
07-24-2009 2:49 PM |
I've just spent 4 hours chasing this problem. Thanks for the suggestions and workarounds. Doesn't a regression like this warrant a hotfix?
07-24-2009 3:11 PM |
This was broken in the Beta. I don't really know why it wasn't fixed for RTM. I have a hard time believing no one could find a way.
07-25-2009 12:02 PM |
unforetuneately I'm using vb, translation of the above solutions is not going well, is there a vb solution for this problem?
andrewaggb
6 points
4 Posts
08-28-2009 12:39 PM |
you could try
http://www.developerfusion.com/tools/convert/csharp-to-vb
or
http://www.carlosag.net/Tools/CodeTranslator/
I've used similiar tools in the past to convert vb to c# (I know you're going the other way) and they work ok. They get 80-90% of it right from my experience.