Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ahopper opened this issue Jun 23, 2023 · 6 comments · Fixed by #11918
Closed

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

ahopper opened this issue Jun 23, 2023 · 6 comments · Fixed by #11918
Labels

Comments

@ahopper
Copy link
Contributor

ahopper commented Jun 23, 2023

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 ahopper added the bug label Jun 23, 2023
@timunie
Copy link
Contributor

timunie commented Jun 23, 2023

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; }
}

@ahopper
Copy link
Contributor Author

ahopper commented Jun 23, 2023

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

@X9VoiD
Copy link

X9VoiD commented Jul 10, 2023

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.

@timunie
Copy link
Contributor

timunie commented Jul 10, 2023

@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.

@X9VoiD
Copy link

X9VoiD commented Jul 10, 2023

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.

@mightypanda
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants