Custom date picker throws exception windows phone

437 Views Asked by At

I am developing a windows phone application. In that I wanted a functionality such that if a text box gets focus date picker should be opened. Similarly another text box will trigger a time picker.

i searched and found that this can be achieved with writing a custom date/time picker. I successfully got the time picker working in this fashion but when I tried to implement the same method for date picker I'm getting the following exception.

"Error HRESULT E_FAIL has been returned from a call to a COM component."

The XAML code for the time and date picker are as follows.

<popUps:CustomTimePicker x:Name="timePicker" Visibility="Collapsed" Value="{Binding SelectedTime, Converter={StaticResource dateTimeConverter}, Mode=TwoWay}" />
<popUps:CustomDatePicker x:Name="datePicker" Visibility="Collapsed" />

The custom date picker class which I wrote for the same is

class CustomDatePicker : DatePicker
{
    public void ClickDateTemplateButton()
    {
        ApplyTemplate();

        Button button = (GetTemplateChild("DateTimeButton") as Button);
        if (button != null)
        {

            ButtonAutomationPeer peer = new ButtonAutomationPeer(button);
            if (peer != null)
            {
                IInvokeProvider provider = (peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider);
                if (provider != null)
                {
                    provider.Invoke();
                }
            }

        }
    }
}

Please help me finding where it went wrong. Thanks.

1

There are 1 best solutions below

2
On BEST ANSWER

As a workaround, don't add the customDatePicker in your listBoxItem DataTemplate. Whenever you want to launch the controls, call the following method. You will need to save the index of your selected listBoxItem in your page's transient State in order to populate the correct Item of your listbox with the selected value, after you return to your page, when the datepicker page closes.

private void LanchDatePicker()
{
    datepicker = new CustomDatePicker
    {
        IsTabStop = false, 
        MaxHeight = 0,
        Value = null
    };

   datepicker.ValueChanged += DatePicker_OnValueChanged;
   LayoutRoot.Children.Add(datepicker);
   datepicker.ClickTemplateButton();
}