【问题标题】:WPF DecimalUpDown custom template NullReferenceExceptionWPF DecimalUpDown 自定义模板 NullReferenceException
【发布时间】:2015-10-04 03:58:27
【问题描述】:

我正在尝试从 WPF 扩展工具包中修改 DecimalUpDown 控件的外观,但每次尝试为其创建模板时,我都会在运行时收到 NullReferenceException。

我尝试使用此链接中的模板:https://social.msdn.microsoft.com/Forums/vstudio/en-US/304c5191-687f-47b9-8924-56816647e2b4/im-using-the-wpf-extended-toolkit-singleupdown-control?forum=wpf

当那不起作用时,我尝试尽可能基本并使用它:

<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>

但我两次都收到了 NullReferenceException。堆栈跟踪是:

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()

我的模板中似乎遗漏了一些强制性的东西,但我不知道它是什么。有人可以帮忙吗?

如果您还可以让我知道如何定义可以应用于所有向上/向下控件的样式(我也在我的应用程序中使用 IntegerUpDown),那么我将非常感激。

提前致谢

【问题讨论】:

  • 尝试使用 x:Name="PART_Spinner" 而不是 Name="PART_Spinner"。
  • 您使用的是哪个版本的工具包?
  • 大家好。我使用的是旧版本的工具包。我已经下载了最新的,它现在可以工作了!感谢您的帮助

标签: wpf xaml numericupdown wpf-extended-toolkit integerupdown


【解决方案1】:

我使用的是旧版本的工具包。下载最新版本后,它现在似乎可以工作了。感谢 Tanis83 提出这个建议。

【讨论】:

    猜你喜欢
    • 2011-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-07
    相关资源
    最近更新 更多