Binding works or not depending order in Xaml

elcoelco Member ✭✭

I've run into this a few times now and I was wondering if these are known issues and if there are any other instances where this is the case:

I have a DataTemplate containing a slider where the value is bound to a property using the OneWay mode (this because the value is essentially a request to change the real value etc).
Now initially I had it bound like this:
<Slider x:Name="LightIntensitySlider" Value="{Binding Content.Value, Mode=OneWay}" HorizontalOptions="Fill" Grid.Row="1" Minimum="0" Maximum="99" />
Which doesn't work. I then set the Mode to TwoWay which does work. I then changed the location of the property in the Xaml like this:
<Slider x:Name="LightIntensitySlider" HorizontalOptions="Fill" Grid.Row="1" Minimum="0" Maximum="99" Value="{Binding Content.Value, Mode=OneWay}" />
Which does work... This cost me hours to accidentally stumble upon. In the cases where it doesn't work it won't even evaluate the Xaml it seems; I added a value converter inbetween and it wasn't being triggered at all.

The other situation I spent way too much time on was the order of binding a bool property to a Button's IsEnabled where the Xaml also contains a binding to a Command (which apparently is a known issue that's been open for YEARS)

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited September 11

    I then changed the location of the property in the Xaml like this ... Which does work...

    I've reproduce your code, the reason for the problem is Slider's value. Maximum is the maximum of the range, with a default value of 1. Minimum is the minimum of the range, with a default value of 0. The Slider coerces the Value property so that it is between Minimum and Maximum, inclusive. If the Minimum property is set to a value greater than the Value property, the Slider sets the Value property to Minimum.

    In your code, you set the value of the Slider before the Minimum and Maximum, so if the value is greater than the default value of Maximum (1), the Value will be set to 1.

    Check the Tutorial:
    https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/slider

  • elcoelco Member ✭✭

    So the order of the properties set in the Xaml code also defines the order the properties are actually set in? I understand why it wouldn't work as expected with the Binding having values outside its range but ignoring the the binding altogether (which is essentially what happens?) also doesn't seem right - i.e. perhaps clamp the incoming value to the range or something.
    Also, why does it work when the mode is set to TwoWay?

  • JarvanJarvan Member, Xamarin Team Xamurai

    So the order of the properties ... are actually set in?... also doesn't seem right

    Yes, you should set Maximum value before Value to Slider.

    why does it work when the mode is set to TwoWay?

    TwoWay data goes both ways between source and target. Have you changed the Content.Value in code behind. Please post more details about the code or sharing a basic demo to reproduce the issue.

Sign In or Register to comment.