Skip to main content

Microsoft Silverlight

Answered Question Datepicker problem or bug?RSS Feed

(0)

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Datepicker problem or bug?

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:

<data:DataGridTemplateColumn Header="Event Date" Width="100">

<data:DataGridTemplateColumn.CellTemplate>

<DataTemplate>

<TextBlock Text="{Binding EventDt, Mode=OneWay, Converter={StaticResource ShortDateConverter}}" VerticalAlignment="Center" Margin="2"/>

</DataTemplate>

</data:DataGridTemplateColumn.CellTemplate>

<data:DataGridTemplateColumn.CellEditingTemplate>

<DataTemplate>

<controls:DatePicker SelectedDate="{Binding EventDt, Mode=TwoWay}" />

</DataTemplate>

</data:DataGridTemplateColumn.CellEditingTemplate>

</data:DataGridTemplateColumn>

basak
basak

Member

Member

174 points

32 Posts

Microsoft

Re: Datepicker problem or bug?

 

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. 

 

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Datepicker problem or bug?

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.

 

 

basak
basak

Member

Member

174 points

32 Posts

Microsoft

Re: Re: Datepicker problem or bug?

It was the same behavior in Silverlight 2. SelectedDate was only committed when Enter key is hit. Can you please post all your project?

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Re: Datepicker problem or bug?

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.

 

 

 

basak
basak

Member

Member

174 points

32 Posts

Microsoft

Re: Re: Re: Datepicker problem or bug?

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.

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Re: Re: Datepicker problem or bug?

Thank you.

 

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft
Answered Question

Re: Datepicker problem or bug?

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;
                });
            }
        }
    }
 

Yifung Lin [MSFT]

avbersSL
avbersSL

Member

Member

164 points

78 Posts

Re: Re: Datepicker problem or bug?

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..)

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Datepicker problem or bug?

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.

Thanks.

yifung
yifung

Contributor

Contributor

3313 points

540 Posts

Microsoft

Re: Datepicker problem or bug?

Thanks, I'm sorry about the inconvenience.  This is definitely one of the worst issues in the SL 3 DataGrid.

Yifung Lin [MSFT]

avbersSL
avbersSL

Member

Member

164 points

78 Posts

Re: Re: Re: Datepicker problem or bug?

I was wondering what the SL team had to say about this issue....

Brian Braeckel
Brian Br...

Member

Member

368 points

66 Posts

Re: Re: Re: Datepicker problem or bug?

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
estern

Member

Member

422 points

126 Posts

Re: Re: Re: Re: Datepicker problem or bug?

I've just spent 4 hours chasing this problem.  Thanks for the suggestions and workarounds.  Doesn't a regression like this warrant a hotfix?

wackyphill
wackyphill

Member

Member

37 points

119 Posts

Re: Re: Re: Re: Datepicker problem or bug?

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.

estern
estern

Member

Member

422 points

126 Posts

Re: Re: Re: Re: Re: Datepicker problem or bug?

unforetuneately I'm using vb, translation of the above solutions is not going well, is there a vb solution for this problem?

andrewaggb
andrewaggb

Member

Member

6 points

4 Posts

Re: vb version

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.

 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities