Skip to content

Hexadecimal not initialized correctly #74

@Justin113D

Description

@Justin113D

If a NumericBox<T> has the NumberStyle HexNumber, and the bound property has a default value other than 0, the initial text is not being parsed correctly.

Example code:

Viewmodel.cs
namespace NumericIssue
{
    public class Viewmodel : INotifyPropertyChanged
    {
        private int _hexValue = 0xBEEF;

        public int HexValue
        {
            get => _hexValue;
            set
            {
                _hexValue = value;
                PropertyChanged.Invoke(this, 
                    new PropertyChangedEventArgs(nameof(HexValue)));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
Window.xaml
<Window x:Class="NumericIssue.Window"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:NumericIssue"
        xmlns:num="http://gu.se/NumericInput"
        Title="Issue" Height="100" Width="100">
    <Window.DataContext>
        <local:Viewmodel/>
    </Window.DataContext>
    
    <StackPanel>
        <num:IntBox Value="{Binding HexValue}"/>
        <num:IntBox Value="{Binding HexValue}" 
                    NumberStyles="HexNumber"/>
    </StackPanel>
</Window>

In this example, I set the initial value of a property to 0xBEEF, which is 48879 as a decimal.
While the value behind both IntBoxes is correct, the text of the second IntBox is 48879, even though it should be BEEF.
By selecting the hexadecimal text, and then the decimal text, the second box interprets the 48879 as a hexadecimal and adjusts the bound property accordingly, which results in a new value of 297081.

Demonstration:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions