Search code examples
wpfxamlnumericupdownwpf-extended-toolkitintegerupdown

WPF DecimalUpDown custom template NullReferenceException


I am trying to modify the appearance of the DecimalUpDown control from the WPF Extended Toolkit but every time I try to create a template for it I get a NullReferenceException at run time.

I tried using the template from this link: https://social.msdn.microsoft.com/Forums/vstudio/en-US/304c5191-687f-47b9-8924-56816647e2b4/im-using-the-wpf-extended-toolkit-singleupdown-control?forum=wpf

When then didn't work I tried just be as basic as possible and used this:

<Style TargetType="{x:Type etk:DecimalUpDown}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="etk:DecimalUpDown">
                <etk:ButtonSpinner
                    Name="PART_Spinner">
                    <etk:WatermarkTextBox
                        Name="PART_TextBox" />
                </etk:ButtonSpinner>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

But I got a NullReferenceException both times. The stack trace is:

at Microsoft.Windows.Controls.Primitives.UpDownBase`1.OnApplyTemplate()
at System.Windows.FrameworkElement.ApplyTemplate()
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV, Boolean& hasDesiredSizeUChanged)
at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)
at System.Windows.Controls.Grid.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
at System.Windows.Controls.ContentPresenter.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Controls.Border.MeasureOverride(Size constraint)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Window.MeasureOverrideHelper(Size constraint)
at System.Windows.Window.MeasureOverride(Size availableSize)
at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
at System.Windows.UIElement.Measure(Size availableSize)
at System.Windows.Interop.HwndSource.SetLayoutSize()
at System.Windows.Interop.HwndSource.set_RootVisualInternal(Visual value)
at System.Windows.Interop.HwndSource.set_RootVisual(Visual value)
at System.Windows.Window.SetRootVisual()
at System.Windows.Window.SetRootVisualAndUpdateSTC()
at System.Windows.Window.SetupInitialState(Double requestedTop, Double requestedLeft, Double requestedWidth, Double requestedHeight)
at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
at System.Windows.Window.CreateSourceWindowDuringShow()
at System.Windows.Window.SafeCreateWindowDuringShow()
at System.Windows.Window.ShowHelper(Object booleanBox)
at System.Windows.Window.Show()
at System.Windows.Window.ShowDialog()

It seems I'm missing something mandatory in my templates but I don't know what it is. Can anyone help?

If you could also let me know how to define a style I can apply to ALL up/down controls (I'm also using the IntegerUpDown in my app too) then I would be very grateful.

Thanks in advance


Solution

  • I was using an old version of the toolkit. After downloading the latest version it now seems to work. Thanks to Tanis83 for suggesting this.