如何显示DatePicker& TimePicker控制包含它们的Popop?

时间:2013-11-26 16:56:34

标签: c# windows-phone-8 windows-phone windows-phone-toolkit

我正在尝试使用Windows Phone Toolkit DatePicker创建一个弹出窗口。 TimePicker,带有以下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>

这样可行,但每次单击其中任何一个时,它们都会在弹出窗口下呈现,例如

enter image description here

在显示DatePicker或TimePicker时,我无法想办法隐藏弹出窗口。

我已经尝试将控件的Z-Index设置为大于Popup,如下所示:

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

我也试过隐藏Popup with

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

但这会关闭弹出窗口和TimePicker / DatePicker。

有没有办法在Popup控件上查看DatePicker / TimePicker控件?

1 个答案:

答案 0 :(得分:1)

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>

背后的代码

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