我使用TimePicker并以默认值“ 12:00 AM”出现,我想将该字段设置为空白。
<TimePicker x:Name="timepicker" />
答案 0 :(得分:0)
您必须创建自己的TimePicker
类,以允许在XAML上使用NullableTime
。
该类应如下:
public class MyTimePicker : TimePicker
{
private string _format = null;
public static readonly BindableProperty NullableDateProperty =
BindableProperty.Create<MyTimePicker, TimeSpan?>(p => p.NullableTime, null);
public TimeSpan? NullableTime
{
get { return (TimeSpan?)GetValue(NullableDateProperty); }
set { SetValue(NullableDateProperty, value); UpdateTime(); }
}
private void UpdateTime()
{
if (NullableTime.HasValue) { if (null != _format) Format = _format; Time = NullableTime.Value; }
else { _format = Format; Format = "pick ..."; }
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
UpdateTime();
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == "Time") NullableTime = Time;
}
}
然后在XAML上,您应该创建控件并按以下方式使用它:
<local:MyTimePicker NullableTime="{x:Null}" />
如果使用此示例,则会看到默认值应为...