How to fix Xceed DateTimePicker ArgumentOutOfRangeException Error

1.4k views Asked by At

I am using the Xceed DateTimePicker as the control in a wpf DataGrid for all columns that are bound to a Date property. Each of the those columns is defined as follows:

    <DataGrid.Columns>
    <DataGridTemplateColumn
    Header="Charge Date"
    Width="100">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock
                Text="{Binding Path=ChargeDate, StringFormat=yyyy-MM-dd, Converter={StaticResource conDate}}"
                HorizontalAlignment="Center" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <tk:DateTimePicker Value="{Binding Path=ChargeDate}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

I don't think it is pertinent to my question, but for completeness sake, here is the styling for the pickers:

<Style TargetType="{x:Type tk:DateTimePicker}">
    <Setter Property="TextAlignment" Value="Center" />
    <Setter Property="Minimum" Value="2017-01-01" />
    <Setter Property="DisplayDefaultValueOnEmptyText" Value="False" />
    <Setter Property="ShowButtonSpinner" Value="False" />
    <Setter Property="TimePickerVisibility" Value="Collapsed" />
    <Setter Property="AutoCloseCalendar" Value="True" />
    <Setter Property="Format" Value="Custom" />
    <Setter Property="FormatString" Value="yyyy-MM-dd" />
</Style>

This works for datagrid cells that are already populated or when I am entering data in a new row. However when I click on an empty cell in an existing row, I get the following exception:

System.ArgumentOutOfRangeException: 'SelectedDate value is not valid.'

Why is the error only when I am enter data in an existing row? No code-behind is being executed when this exception happens so I don't know where to look for the problem.

2

There are 2 answers

4
mm8 On

Since you have set the Minimum property to 2017-01-01, you should also set the default value to the same date:

<Style TargetType="{x:Type tk:DateTimePicker}">
    <Setter Property="TextAlignment" Value="Center" />
    <Setter Property="Default" Value="2017-01-01" />
    <Setter Property="Minimum" Value="2017-01-01" />
    <Setter Property="DisplayDefaultValueOnEmptyText" Value="False" />
    <Setter Property="ShowButtonSpinner" Value="False" />
    <Setter Property="TimePickerVisibility" Value="Collapsed" />
    <Setter Property="AutoCloseCalendar" Value="True" />
    <Setter Property="Format" Value="Custom" />
    <Setter Property="FormatString" Value="yyyy-MM-dd" />
</Style>

You will get an ArgumentOutOfRangeException if the default value or value is less than the minimum value and this makes perfect sense.

0
Dries Geenen On

I had the same issue. To fix this set the ClipValueToMinMax property to "True". This will prevent the value from going below minimum/above maximum without throwing an exception.

<xceed:DateTimePicker Value="{Binding DateTime}"
                      Minimum="{Binding DateTimeMinimum}"
                      Maximum="{Binding DateTimeMaximum}"
                      ClipValueToMinMax="True"/>