Skip to content

Binding Error - "Value 'null' cannot be assigned to property TextBox.Name" in Control Template #72

@newsoneisb

Description

@newsoneisb

Description

There is a recurring binding error in the current version of the NumericUpDown control. When the control is used in a WPF application, it produces the following error in the XAML binding failure logs:

Error: Value 'null' cannot be assigned to property TextBox.Name, Name='PART_TextBox' (type String).
Error: Value 'null' cannot be assigned to property TextBox.Name, Name='PART_Measuring_Element' (type String).

This error is related to a MultiBinding used to set the AutomationProperties.Name on the inner TextBox elements (PART_TextBox and PART_TART_Measuring_Element) within the control's template. The binding path is reported as (0), which indicates that the first binding in the MultiBinding is resolving to null.

This typically happens when the AutomationProperties.Name of the NumericUpDown control itself is not explicitly set, causing the RelativeSource binding to resolve to null.

The problematic binding is located in NumericUpDownLib/Themes/Generic.xaml.

Proposed Fix

To prevent this error, a FallbackValue should be added to the binding. This ensures that even if the AutomationProperties.Name is not set on the parent control, the binding will fall back to an empty string instead of null, thus preventing the assignment of a null value to a String property.

Here is the code change for NumericUpDownLib/Themes/Generic.xaml:

--- a/NumericUpDownLib/Themes/Generic.xaml
+++ b/NumericUpDownLib/Themes/Generic.xaml
@@ -475,7 +475,8 @@
                                     <MultiBinding StringFormat="{}{0} {1}">
                                         <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                  Path="(AutomationProperties.Name)"
-                                                 UpdateSourceTrigger="PropertyChanged" />
+                                                 UpdateSourceTrigger="PropertyChanged"
+                                                 FallbackValue=""/>
                                         <Binding ElementName="PART_TextBox"
                                                  Path="Text"
                                                  UpdateSourceTrigger="PropertyChanged" />
@@ -503,7 +504,8 @@
                                     <MultiBinding StringFormat="{}{0} {1}">
                                         <Binding RelativeSource="{RelativeSource TemplatedParent}"
                                                  Path="(AutomationProperties.Name)"
-                                                 UpdateSourceTrigger="PropertyChanged" />
+                                                 UpdateSourceTrigger="PropertyChanged"
+                                                 FallbackValue=""/>
                                         <Binding ElementName="PART_Measuring_Element"
                                                  Path="Text"
                                                  UpdateSourceTrigger="PropertyChanged" />

This change is non-disruptive and improves the robustness of the control by ensuring it does not produce binding errors when used in standard scenarios.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions