How to display NaN/Inf within NumericUpDown

3 Answers 126 Views
NumericUpDown
Lukasz
Top achievements
Rank 1
Iron
Lukasz asked on 16 Jun 2022, 04:48 PM | edited on 16 Jun 2022, 04:49 PM

I have a problem with RadNumericUpDown control. I'm not able to display edge values of double type (e.g. NaN or Infinity). Every time I try to bind to property with these values I've got a Binding error.

Is it even possible to achieve it utilizing RadNumericUpDown?

Thanks!

Stenly
Telerik team
commented on 21 Jun 2022, 12:30 PM

Would it be possible to provide a bit more information regarding the desired result as I am not sure that I fully understand this requirement?
Lukasz
Top achievements
Rank 1
Iron
commented on 22 Jun 2022, 05:18 PM | edited

For NaN/+Inf/-Inf etc on view model side, I just want to have the same value within control. Currently I'm not able to do this in RadNumericUpDown.

Example:
- vm: NaN -> control: NaN
- vm: +Inf -> control: +Inf
etc.

 

public class ViewModel : INotifyPropertyChanged

{

      public double? Value {get; set;}

}

3 Answers, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 27 Jun 2022, 10:49 AM

Hello Lukasz,

You can achieve the desired result by utilizing the NullValue property of the RadNumericUpDown control and binding it to a property of your viewmodel. Here's what I have in mind:

        public double? Value
        {
            get
            {
                return this.value;
            }
            set
            {
                this.value = value;
                this.RaisePropertyChanged();
                if (this.value != null && double.IsNaN(this.value.Value))
                {
                    this.NullValue = "NaN";
                }
                else if (this.value == double.PositiveInfinity)
                {
                    this.NullValue = "+Inf";
                }
                else if (this.value == double.NegativeInfinity)
                {
                    this.NullValue = "-Inf";
                }
            }
        }

Attached you can find a small sample project which demonstrates this in action. Please have a look and let me know if this works for you.

Regards,
Dilyan Traykov
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

0
Lukasz
Top achievements
Rank 1
Iron
answered on 27 Jun 2022, 04:09 PM | edited on 28 Jun 2022, 05:01 PM

Dilyan, thank you for your quickanswear!

I'll let you know whether it's ok or not.

Regards!

0
Lukasz
Top achievements
Rank 1
Iron
answered on 28 Jun 2022, 05:00 PM | edited on 28 Jun 2022, 05:07 PM

Unfortunately, it was not my intention to have these properties within view model, but within the control (by overriding properties or methods). I have thousands of view models when I have to to introduce such a thing, so there is no way to do this by changing view model this way. Am I able to achieve it by creating the new control inheriting from RadNumericUpDown?

 

I tried to override RadNumericUpDown.FormatDisplay() method, but it does not allow to bind NaN or Inf anyway, so I've got a null value within Value property.

 

Regards!

Dilyan Traykov
Telerik team
commented on 30 Jun 2022, 10:11 AM

Hello Lukasz,

The Value dependency property of the RadNumericUpDown has a ValidateValueCallback which performs the following check:

        private static bool IsValidDoubleValue(object value)
        {
            if (value == null)
                return true;

            double num = (double)value;
            return !DoubleUtil.IsNaN(num) && !double.IsInfinity(num);
        }

That is why the NaN and Infinity values cannot be bound.

Indeed, you may be able to achieve the desired result by inheriting from RadNumericUpDown and exposing additional properties. You may also need to edit the control template of the new control, however, to achieve the desired result.

Tags
NumericUpDown
Asked by
Lukasz
Top achievements
Rank 1
Iron
Answers by
Dilyan Traykov
Telerik team
Lukasz
Top achievements
Rank 1
Iron
Share this question
or