Search code examples
c#.netsilverlightwindows-phone-7silverlight-toolkit

TimePicker without the TextBox


I want to use only the chooser dialog (shown below)

enter image description here

from the TimePicker control included in the Silverlight Toolkit for Windows Phone. Normally this dialog only appears when a user clicks on the TimePicker control listbox.

enter image description here

I would like to bypass the listbox altogether and launch the chooser dialog when a button is pressed.

Is this possible or would I have to create a custom control for myself.


Solution

  • Create class that inherited from TimePicker, and use ClickTemplateButton() to simulate click behavior:

    public class CustomPicker : TimePicker
    {
        public void ClickTemplateButton()
        {
            Button button = (GetTemplateChild("DateTimeButton") as Button);
            ButtonAutomationPeer peer = new ButtonAutomationPeer(button);
            IInvokeProvider provider = (peer.GetPattern(PatternInterface.Invoke) as IInvokeProvider);
    
            provider.Invoke();
        } 
    }
    

    When this class is created, create CustomPicker in xaml. Don't forget to add xmlns

     xmlns:local="clr-namespace:CustomPickerNamespace"
    
    
     <local:CustomPicker x:Name="customPicker" .../>
    

    And then call you can show it from code:

     customPicker.ClickTemplateButton()