Reputation: 101
In the program I have one DatePicker
that the user are allowed to not enter a date. But if the user enters one date and then he deletes it, the TextBox
in the DatePicker
does some kind of validation, and I want to disable that. Also, if I save the value after this "validation" the date entered before is the one saved.
I have tried to set the date as null if it doesn't have any text, but that doesn't work.
private void PART_TextBoxFim_TextChanged(object sender, TextChangedEventArgs e)
{
if (Dp_DataFim.Text == null)
{
Dp_DataFim.SelectedDate = null;
Dp_DataFim.DisplayDate = DateTime.Today;
}
}
Inside the class of the page I have this:
public Nullable<DateTime> Prop { get; set; }
The XAML of the datepicker is this one:
<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>
I have noticed when this happens, I get a error like this one:
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)'
Upvotes: 1
Views: 1395
Reputation: 169180
Bind the SelectedDate
property of the DatePicker
to a Nullable<DateTime>
source property.
If you change the format of the date by creating a custom ControlTemplate
for the DatePickerTextBox
where you bind its Text
property to the SelectedDate
property, you could then use a simple converter class to convert the empty string
to a 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?);
}
}
Usage:
<DatePicker x:Name="Dp_DataFim" SelectedDate="{Binding MyProp}">
<DatePicker.Resources>
<local:StringToDateTimeConverter x:Key="StringToDateTimeConverter" />
<Style TargetType="{x:Type DatePickerTextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<TextBox x:Name="PART_TextBox" Text="{Binding Path=SelectedDate,
RelativeSource={RelativeSource AncestorType={x:Type DatePicker}},
Converter={StaticResource StringToDateTimeConverter}}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DatePicker.Resources>
</DatePicker>
Upvotes: 2