Avalonia icon indicating copy to clipboard operation
Avalonia copied to clipboard

Slider clamps bound value to default min max value when removed from visual tree

Open ahopper opened this issue 1 year ago • 5 comments

Describe the bug A slider or any RangeBase control with Value, Minimum and Maximum bound writes a value back to the binding clamped to the default (0 ,100) rather than the bound min max.

To Reproduce Steps to reproduce the behavior: Run the repro and click flip twice, The slider will change value when it should not

RangeBaseRepro.zip

Expected behavior The view model should maintain the requested value.

Desktop (please complete the following information):

  • OS: Windows 10
  • Version 11.0.999-cibuild0036924-beta

ahopper avatar Jun 23 '23 10:06 ahopper

Here is a falling test for this

[Fact]
public void ChangingDataContextShouldNotChangeOldDataContext()
{
    var viewModel = new RangeTestViewModel()
    {
        Minimum = -5000, 
        Maximum = 5000, 
        Value = 4000
    };
    
    var target = new TestRange
    {
        [!RangeBase.MinimumProperty] = new Binding(nameof(viewModel.Minimum)),
        [!RangeBase.MaximumProperty] = new Binding(nameof(viewModel.Maximum)),
        [!RangeBase.MaximumProperty] = new Binding(nameof(viewModel.Value)),
    };
    
    var root = new TestRoot(target);
    target.DataContext = viewModel;
    target.DataContext = null;
    
    Assert.Equal(4000, target.Value);
    Assert.Equal(-5000, target.Minimum);
    Assert.Equal(5000, target.Maximum);
}

private class RangeTestViewModel
{
    public double Minimum { get; set; }
    public double Maximum { get; set; }
    public double Value { get; set; }
}

timunie avatar Jun 23 '23 13:06 timunie

Your test seems to set Maximum Property twice rather than value. btw this is a recent regression/issue

ahopper avatar Jun 23 '23 14:06 ahopper

This bug also happens when binding to a nested viewmodel (or any observable object/property) that is set later.

<AnyRangeBase Value="{Binding NestedViewModel.Value}"/>

xaml above is just to clarify what I meant with nested view model.

X9VoiD avatar Jul 10 '23 10:07 X9VoiD

@X9VoiD can you check if my PR (#11918) would resolve your issue? I still need a review from @grokys before this can be merged, but still would be useful to have your tests performed.

timunie avatar Jul 10 '23 11:07 timunie

~~Unfortunately that PR did not fix my issue. I don't even have to set the DataContext to null after.~~

After some fiddling it was an issue with my XAML, where I set Value first before the range. ~~The PR works! 🎉~~

Seems like what I did also fixed the issue without using this PR. I'd say my issue was a PEBKAC and is different from this one.

X9VoiD avatar Jul 10 '23 15:07 X9VoiD

This bug is driving me mad, i get a "100" set value in when, for example, deselecting an element where a Slider was present. How can i fix it?

mightypanda avatar Dec 01 '23 14:12 mightypanda