在程序中,我有一个DatePicker
,允许用户不输入日期。但是,如果用户输入一个日期然后将其删除,则TextBox
中的DatePicker
会进行某种验证,我想禁用该日期。另外,如果我在“验证”之后保存值,那么之前输入的日期就是保存的日期。
如果没有任何文本,我试图将日期设置为null,但这是行不通的。
private void PART_TextBoxFim_TextChanged(object sender, TextChangedEventArgs e)
{
if (Dp_DataFim.Text == null)
{
Dp_DataFim.SelectedDate = null;
Dp_DataFim.DisplayDate = DateTime.Today;
}
}
在页面类中,我有这个:
public Nullable<DateTime> Prop { get; set; }
datepicker的XAML是这样的:
<DatePicker x:Name="Dp_DataFim" Grid.Row="0" Grid.Column="2" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,250,0,0" FontSize="16" Height="20" Width="230" SelectedDate="{Binding Prop}" IsTodayHighlighted="False" SelectedDateFormat="Short" BorderThickness="0" Padding="0" BorderBrush="{x:Null}" IsTabStop="True" SelectedDateChanged="Dp_DataFim_SelectedDateChanged" Visibility="Hidden">
<DatePicker.Resources>
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Control.Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}, StringFormat='dd/MM/yyyy HH:mm:ss'}" Background="#FF494949" Foreground="#FFEEEEEE" BorderThickness="0" IsReadOnly="False" TextChanged="PART_TextBoxFim_TextChanged"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
我注意到这种情况发生时,出现了这样的错误:
System.Windows.Data Error: 7 : ConvertBack cannot convert value '' (type 'String'). BindingExpression:Path=SelectedDate; DataItem='DatePicker' (Name='Dp_DataFim'); target element is 'TextBox' (Name='PART_TextBox'); target property is 'Text' (type 'String') FormatException:'System.FormatException: The string was not recognized as a valid DateTime value.
em System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles)
em System.Convert.ToDateTime(String value, IFormatProvider provider)
em System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
em System.Windows.Data.BindingExpression.ConvertBackHelper(IValueConverter converter, Object value, Type sourceType, Object parameter, CultureInfo culture)'
答案 0 :(得分:1)
将SelectedDate
的{{1}}属性绑定到DatePicker
源属性。
如果通过为Nullable<DateTime>
创建自定义ControlTemplate
并将其DatePickerTextBox
属性绑定到Text
属性的方式来更改日期格式,则可以使用一个简单的转换器类,将空的SelectedDate
转换为string
:
default(DateTime?)
用法:
public class StringToDateTimeConverter : IValueConverter
{
private const string DateFormat = "dd/MM/yyyy HH:mm:ss";
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
DateTime? dt = (DateTime?)value;
return dt.HasValue ? dt.Value.ToString(DateFormat, CultureInfo.InvariantCulture) : string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string s = value.ToString();
if (string.IsNullOrEmpty(s))
return default(DateTime?);
DateTime dt;
return DateTime.TryParseExact(s, DateFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out dt) ? dt : default(DateTime?);
}
}