Reputation: 105
In my UWP application under Windows 10 build 18362, I have a ListView that contains a CalendarDatePicker. The value displayed in the field of this control is stored in a SQLite database in text format. After reading this text field (format yyyyMMdd), I convert it to DateTimeOffset format with a converter, and it works fine. My problem occurs when the date field is null (the user did not choose a date in the CalendarDatePicker before saving their data). Then, at the opening of the page, the CalendarDatePicker always displays a default date that is 01/01/1919, whereas it should offer me the PlaceholderText defined. Here are excerpts from the XAML code of the page and the code behind of the Converter. Any help will be welcome. Thank you.
<ListView x:Name="listVProjets"
Margin="0,140,0,40"
IsItemClickEnabled="True"
ItemClick="ListVProjets_ItemClick"
SelectionChanged="ListVProjets_SelectionChanged"
>
<ListView.ItemTemplate>
<DataTemplate x:DataType="local1:Projets">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="40" />
<ColumnDefinition Width="40" />
<ColumnDefinition Width="240" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="150" />
<ColumnDefinition Width="110" />
<ColumnDefinition Width="110" />
</Grid.ColumnDefinitions>
<Button Grid.Column="0"
x:Name="btnOpenSProj"
Content="--"
Tag="{Binding}"
Click="BtnOpenSProj_Click"/>
<CheckBox Grid.Column="1"
x:Name="chkBoxProj"
Margin="15,0,0,0"
Tag="{Binding}"
Click="ChkBoxProj_Click"
IsChecked="{Binding IsChk, Converter={StaticResource NullableBooleanToBooleanConverter}, Mode=OneWay}"/>
<TextBox Grid.Column="2"
x:Name="nomProjTxt"
Margin="20,0,0,0"
TextWrapping="NoWrap"
BorderBrush="Transparent"
Text="{Binding NomProj, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
FontSize="16"
Foreground="Black"
PlaceholderText="Nom du projet"
SelectionChanged="NomProjTxt_SelectionChanged"/>
<CalendarDatePicker Grid.Column="3"
Name="dateprojPTxt"
Margin="20,0,0,0"
BorderBrush="Transparent"
FontSize="16"
Foreground="Black"
IsTodayHighlighted="True"
DateChanged="DateprojPTxt_DateChanged"
Tag="{Binding}"
Date="{x:Bind DateProjPrevu, Converter={StaticResource StringToDateTimeOffsetConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" DateFormat="{}{day.integer(2)}/{month.integer(2)}/{year.full}"
/>
public class StringToDateTimeOffsetConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
DateTimeOffset dt;
if (value != null && value is string)
{
var stringToConvert = value as string;
if (!DateTimeOffset.TryParse(stringToConvert, out dt))
{
return null;
}
}
return dt;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
string time;
if (value != null && value is DateTimeOffset)
{
var valueToConvert = (DateTimeOffset)value;
time = new DateTime(valueToConvert.Ticks).ToString("yyyyMMdd");
}
else
{
time = null;
}
return time;
}
}
Upvotes: 0
Views: 240
Reputation: 5868
When your date field is null, you will return the DateTimeOffset dt from the Converter. The dt has the default value, so your CalendarDatePicker always displays a default data. So you should return null from the converter when your date field is null like below.
public object Convert(object value, Type targetType, object parameter, string language)
{
DateTimeOffset dt;
if (value != null && value is string)
{
var stringToConvert = value as string;
if (!DateTimeOffset.TryParse(stringToConvert, out dt))
{
return null;
}
}
else if (value == null) {
return null;
}
return dt;
}
Upvotes: 0