Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
This repository was archived by the owner on May 1, 2024. It is now read-only.

[Core] Slider unable to update Maximum , Minimum by binding #2761

@myroot

Description

@myroot

Description

Can't update the Maximum/Mnimum using binding
Becuase validateValue

public static readonly BindableProperty MinimumProperty = BindableProperty.Create("Minimum", typeof(double), typeof(Slider), 0d, validateValue: (bindable, value) =>
{
var slider = (Slider)bindable;
return (double)value < slider.Maximum;
}, coerceValue: (bindable, value) =>

public static readonly BindableProperty MaximumProperty = BindableProperty.Create("Maximum", typeof(double), typeof(Slider), 1d, validateValue: (bindable, value) =>
{
var slider = (Slider)bindable;
return (double)value > slider.Minimum;

validateValue make hard to update value, according to value, Value update order should be changed. like below

   if (max > Minimum)
   {
        Maximum = max;
        Minimum = min;
   }
   else
   {
        Minimum = min;
        Maximum = max;
   }

But it is impossible in binding

So, I propose remove validateValue handler and update coerceValue like below

		public static readonly BindableProperty MinimumProperty = BindableProperty.Create("Minimum", typeof(double), typeof(Slider), 0d, BindingMode.OneWay, coerceValue: (bindable, value) =>
		{
			var slider = (Slider)bindable;
			slider.Value = Math.Max((double)value, slider.Value);
			return value;
		}, propertyChanged:(bindable, oldValue, newValue) =>
		{
			var slider = (Slider)bindable;
			slider.SetValueCore(MaximumProperty, Math.Max(slider.Maximum, (double)newValue));
		});

		public static readonly BindableProperty MaximumProperty = BindableProperty.Create("Maximum", typeof(double), typeof(Slider), 1d, BindingMode.OneWay, coerceValue: (bindable, value) =>
		{
			var slider = (Slider)bindable;
			slider.Value = Math.Min((double)value, slider.Value);
			return value;
		}, propertyChanged:(bindable, oldvalue, newValue) =>
		{
			var slider = (Slider)bindable;
			slider.SetValueCore(MinimumProperty, Math.Min(slider.Minimum, (double)newValue));
		});

Steps to Reproduce

    class RangeModel
    {
        public double Max { get; set; }
        public double Min { get; set; }
    }

    Slider slider = new Slider();
    slider.SetBinding(Slider.MaximumProperty, new Binding("Max"));
    slider.SetBinding(Slider.MinimumProperty, new Binding("Min"));
    slider.BindingContext = new RangeModel { Min = -100, Max = -1 }; // ArgumentException was thrown

Expected Behavior

Update Minimum and Maximum

Actual Behavior

ArgumentException was thrown (Value was an invalid value for Maximum)

Metadata

Metadata

Assignees

No one assigned

    Labels

    in-progressThis issue has an associated pull request that may resolve it!t/bug 🐛

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions