Category / Section
How to bind value in Xamarin.Forms NumerictextBox?
To bind the value in NumericTextBox using MVVM pattern, follow the given steps:
Step 1: Create the Xamarin forms sample.
Step 2: Install the NumericTextBox NuGet from nuget.org and include the NumericTextBox namespace.
Step 2: Create a ViewModel class and add a property to bind the Value property in NumericTextBox as mentioned in the below code sample.
[C#]:
//ViewModel class
public class ViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private float _value=5;
public float Value
{
get{ return _value;}
set{_value = value;RaisePropertyChanged ();}
}
protected void RaisePropertyChanged([CallerMemberName]string propertyName = "")
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
The ViewModel class should extends INofityPropertyChanged, implement its event and raised from Value's property setter as like above snippet, then only we can get the property changing notifications.
Step 3: Set the BindingContext to ViewModel.
[XAML]:
<ContentPage.BindingContext> <local:ViewModel /> </ContentPage.BindingContext>
Step 4: Initialize the SfNumericTextBox control and bind the value property as shown in the below code sample.
[XAML]:
<ContentPage.Content>
<StackLayout >
<syncfusion:SfNumericTextBox x:Name="numericTextBox"
Value="{Binding Value, Mode=TwoWay}"
HorizontalOptions="Center"
VerticalOptions="Center" />
</StackLayout>
</ContentPage.Content>