每次用户点按文本框时,我都想打开一个时间戳选择器。可能吗?我该怎么做?
我找到了帖子:C# WP8 Open TimePicker from code
但是我无法让它工作,因为视觉工作室说" PickerPageUri"方法不可用或不存在。
答案 0 :(得分:2)
每次用户点按文本框时,我都想打开一个时间戳选择器。有可能吗?
是的,这是可能的。
您可以使用TimePickerFlyout执行此操作,例如:
XAML代码:
<TextBox VerticalAlignment="Center" PointerEntered="TextboxPointEntered">
<FlyoutBase.AttachedFlyout>
<TimePickerFlyout />
</FlyoutBase.AttachedFlyout>
</TextBox>
和背后的代码:
private void TextboxPointEntered(object sender, PointerRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout(sender as TextBox);
}
如果您想按代码添加FlyoutBase
,可以这样做:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
TextBox tbox = new TextBox();
tbox.VerticalAlignment = VerticalAlignment.Center;
tbox.PointerEntered += Tbox_PointerEntered;
TimePickerFlyout timepickerFlyout = new TimePickerFlyout();
FlyoutBase.SetAttachedFlyout(tbox, timepickerFlyout);
rootGrid.Children.Add(tbox);
}
private void Tbox_PointerEntered(object sender, PointerRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout(sender as TextBox);
}
此代码中的 rootGrid
代表当前页面的Grid
名称。
要显示TimePicker
中TextBox
的所选时间,您可以这样做:
private TimePickerFlyout timepickerFlyout = new TimePickerFlyout();
private TextBox tbox = new TextBox();
protected override void OnNavigatedTo(NavigationEventArgs e)
{
tbox.VerticalAlignment = VerticalAlignment.Center;
tbox.PointerEntered += Tbox_PointerEntered;
FlyoutBase.SetAttachedFlyout(tbox, timepickerFlyout);
timepickerFlyout.Closed += TimepickerFlyout_Closed;
rootGrid.Children.Add(tbox);
}
private void Tbox_PointerEntered(object sender, PointerRoutedEventArgs e)
{
FlyoutBase.ShowAttachedFlyout(tbox);
}
private void TimepickerFlyout_Closed(object sender, object e)
{
tbox.Text = timepickerFlyout.Time.ToString();
}