Skip to content

Commit 018b07c

Browse files
committed
Fixed the OneWayBinding issue with MultiBindingConverter on Slider and Stepper Value
1 parent 77292fe commit 018b07c

File tree

5 files changed

+154
-2
lines changed

5 files changed

+154
-2
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<ContentPage
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:local="clr-namespace:Maui.Controls.Sample.Issues"
6+
x:Class="Maui.Controls.Sample.Issues.Issue28208">
7+
8+
<ContentPage.Resources>
9+
<local:Issue28208OneWayMultiBindingValueConverter x:Key="ValueConverter"/>
10+
</ContentPage.Resources>
11+
12+
<ScrollView>
13+
<VerticalStackLayout
14+
Padding="30,0"
15+
Spacing="25">
16+
17+
<Slider x:Name="slider"
18+
IsEnabled="False"
19+
Maximum="100">
20+
<Slider.Value>
21+
<MultiBinding Converter="{StaticResource ValueConverter}"
22+
Mode="OneWay">
23+
<Binding Path="Price"/>
24+
</MultiBinding>
25+
</Slider.Value>
26+
</Slider>
27+
28+
<HorizontalStackLayout>
29+
<Label Text="Slider Value : "/>
30+
<Label AutomationId="Sliderlabel"
31+
Text="{Binding Source={x:Reference slider},Path=Value}"/>
32+
</HorizontalStackLayout>
33+
34+
<Stepper x:Name="stepper"
35+
IsEnabled="False">
36+
<Stepper.Value>
37+
<MultiBinding Converter="{StaticResource ValueConverter}"
38+
Mode="OneWay">
39+
<Binding Path="Price"/>
40+
</MultiBinding>
41+
</Stepper.Value>
42+
</Stepper>
43+
44+
<HorizontalStackLayout>
45+
<Label Text="Stepper Value : "/>
46+
<Label AutomationId="StepperLabel"
47+
Text="{Binding Source={x:Reference stepper},Path=Value}"/>
48+
</HorizontalStackLayout>
49+
50+
<Button
51+
Text="Increase ViewModel Price Value"
52+
AutomationId="Button"
53+
Clicked="OnCounterClicked"
54+
HorizontalOptions="Fill"/>
55+
</VerticalStackLayout>
56+
</ScrollView>
57+
58+
</ContentPage>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System.ComponentModel;
2+
using System.Globalization;
3+
4+
namespace Maui.Controls.Sample.Issues
5+
{
6+
[Issue(IssueTracker.Github, 28208, "[Windows] The Slider and Stepper control does not work in One-Way binding mode with a MultiBinding Converter", PlatformAffected.UWP)]
7+
public partial class Issue28208 : ContentPage
8+
{
9+
Issue28208ViewModel _vm;
10+
public Issue28208()
11+
{
12+
InitializeComponent();
13+
this.BindingContext = _vm = new Issue28208ViewModel();
14+
}
15+
16+
private void OnCounterClicked(object sender, EventArgs e)
17+
{
18+
if (_vm is not null)
19+
{
20+
_vm.Price++;
21+
}
22+
}
23+
}
24+
25+
public class Issue28208ViewModel : INotifyPropertyChanged
26+
{
27+
private double _price = 2;
28+
29+
public double Price
30+
{
31+
get => _price;
32+
set
33+
{
34+
_price = value;
35+
OnPropertyChanged(nameof(Price));
36+
}
37+
}
38+
public event PropertyChangedEventHandler PropertyChanged;
39+
40+
private void OnPropertyChanged(string propertyName)
41+
{
42+
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
43+
}
44+
}
45+
46+
public class Issue28208OneWayMultiBindingValueConverter : IMultiValueConverter
47+
{
48+
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
49+
{
50+
if (values[0] is double price)
51+
{
52+
return price;
53+
}
54+
55+
return 0.0;
56+
}
57+
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
58+
{
59+
throw new NotImplementedException();
60+
}
61+
}
62+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NUnit.Framework;
2+
using UITest.Appium;
3+
using UITest.Core;
4+
5+
namespace Microsoft.Maui.TestCases.Tests.Issues
6+
{
7+
public class Issue28208 : _IssuesUITest
8+
{
9+
public Issue28208(TestDevice device) : base(device) { }
10+
11+
public override string Issue => "[Windows] The Slider and Stepper control does not work in One-Way binding mode with a MultiBinding Converter";
12+
13+
[Test]
14+
[Category(UITestCategories.Slider)]
15+
[Category(UITestCategories.Stepper)]
16+
public void OneWayBindingWithMultiBindingConverterShouldReflecttInView()
17+
{
18+
App.WaitForElement("Button");
19+
App.Tap("Button");
20+
App.Tap("Button");
21+
var sliderLabel = App.FindElement("Sliderlabel").GetText();
22+
Assert.That(sliderLabel, Is.EqualTo("4"));
23+
var stepperLabel = App.FindElement("StepperLabel").GetText();
24+
Assert.That(stepperLabel, Is.EqualTo("4"));
25+
}
26+
}
27+
}

src/Core/src/Handlers/Slider/SliderHandler.Windows.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ void OnPlatformViewLoaded(object sender, RoutedEventArgs e)
108108

109109
void OnPlatformValueChanged(object? sender, RangeBaseValueChangedEventArgs e)
110110
{
111-
if (VirtualView != null)
111+
if (VirtualView != null && VirtualView.Value != e.NewValue)
112+
{
112113
VirtualView.Value = e.NewValue;
114+
}
113115
}
114116

115117
void OnPointerPressed(object? sender, PointerRoutedEventArgs e)

src/Core/src/Handlers/Stepper/StepperHandler.Windows.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ void OnValueChanged(object sender, EventArgs e)
5252
if (VirtualView == null || PlatformView == null)
5353
return;
5454

55-
VirtualView.Value = PlatformView.Value;
55+
if (VirtualView.Value != PlatformView.Value)
56+
{
57+
VirtualView.Value = PlatformView.Value;
58+
}
5659
}
5760
}
5861
}

0 commit comments

Comments
 (0)