Question

I am getting a 'System.NullReferenceException' occurred in Xceed.Wpf.Toolkit.dll, exception, at stack location Xceed.Wpf.Toolkit.Core.Primitives.DateTimeUpDownBase`1.OnPreviewKeyDown().

This happens when I enter a date, in a valid format, and hit the enter key.

I have seen a few documented errors that are similar to this, but were due to a Xceed.Wpf.Toolkit v1.9 bug and has been since fixed. I am using v2.2 . Below is my xaml for the DateTimePicker in question.

<Grid DockPanel.Dock="Left">
    <xctk:DateTimePicker
                    BorderThickness="0"
                    Format="Custom"
                    FormatString="{lex:LocText MAIN.Localization:Filters:DateRangeFilterFormatString}"
                    HorizontalAlignment="Right"
                    HorizontalContentAlignment="Stretch"
                    Margin="0,0,1,0"
                    ShowButtonSpinner="False"
                    TimePickerVisibility="Hidden"   
                    Value="{Binding MinimumDate, UpdateSourceTrigger=PropertyChanged}"
                    VerticalAlignment="Center"
                    Keyboard.PreviewKeyDown="keyDownEventHandler"
                    Watermark="{lex:LocText MAIN.Localization:Filters:DateRangeFilterWatermark}"
                    Width="100"/>
</Grid>

EDIT:

I actually dont want the enter keypress to do anything on this control anyways, so I'm attempting to catch the event. I added a keyboard event in the above code. It is catching the events when im typing in a date, but when I hit enter, the error is happening before it even catches the enter key event.

Was it helpful?

Solution

For the record, I work w/ OP, and we figured it out. Just want to answer the question in case others run into it.

The DateTimePicker's default control template has a WatermarkTextBox in it (PART_TextBox) that represents the underlying text area. In the OnPreviewKeyDown handler of the DateTimePicker's base class, the code looks for the binding on the "Text" attribute of that text box, like so:

BindingExpression binding = BindingOperations.GetBindingExpression( TextBox, System.Windows.Controls.TextBox.TextProperty );
binding.UpdateSource();

In our custom template, we were not setting the Text attribute of the WatermarkTextBox, so that meant binding was null in the code above.

We fixed it by adding a binding to the Text attribute of PART_TextBox in our custom DateTimePicker template, so now it looks like this:

<xctk:WatermarkTextBox x:Name="PART_TextBox" 
                       Background="Transparent" 
                       BorderBrush="Transparent" 
                       Grid.Column="0" 
                       HorizontalAlignment="Stretch" 
                       VerticalAlignment="Center" 
                       TextWrapping="NoWrap" 
                       Watermark="{TemplateBinding Watermark}"
                       Text="{Binding Text, RelativeSource={RelativeSource TemplatedParent}}"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top