Search code examples
c#windows-phone-8windows-phonewindows-phone-toolkit

How to show DatePicker & TimePicker controls over the Popop that contains them?


I'm trying to create a popup with Windows Phone Toolkit DatePicker & TimePicker, with the following XAML:

<Popup x:Name="MyPopup">

    <Border BorderThickness="2" Margin="10" BorderBrush="{StaticResource PhoneAccentBrush}">

        <StackPanel>

            <toolkit:DatePicker Name="datePicker" />
            <toolkit:TimePicker Name="timePicker"/>

        </StackPanel>

    </Border>
</Popup>

This works, but every time I click either of them, they are rendered under the popup, e.g.

enter image description here

I can't figure out any way to hide the Popup while displaying the DatePicker or TimePicker.

I've tried setting the Z-Index of the controls to be greater than the Popup, as follows:

void timePicker_GotFocus(object sender, RoutedEventArgs e)
{
    Canvas.SetZIndex(timePicker, Canvas.GetZIndex(MyPopup) + 1);
}

I've also tried hiding the Popup with

void timePicker_GotFocus(object sender, RoutedEventArgs e)
{
    Popup.IsOpen = false;
}

but this closes the popup and the TimePicker/DatePicker.

Is there any way to view the DatePicker/TimePicker controls on top of the Popup control?


Solution

  • Sample project.

    XAML

        <Border
            Background="{StaticResource PhoneChromeBrush}"
            BorderThickness="2"
            Margin="10"
            BorderBrush="{StaticResource PhoneAccentBrush}">
    
            <StackPanel>
    
                <toolkit:DatePicker
                    Tap="DatePickerTap" />
    
                <toolkit:TimePicker
                    Tap="DatePickerTap" />
    
            </StackPanel>
    
        </Border>
    </Popup>
    

    Code behind

    private bool pickerIsOpen = false;
    
    // constructor
    public MainPage()
    {
        InitializeComponent();
    
        this.Loaded += this.MainPageLoaded;
    }
    
    // page loaded
    private void MainPageLoaded(object sender, RoutedEventArgs e)
    {
        if (this.pickerIsOpen)
        {
            this.MyPopup.IsOpen = true;
            this.pickerIsOpen = false;
        }
    }
    
    // date/time picker tap
    private void DatePickerTap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        this.pickerIsOpen = true;
    }