Category / Section
How to bind the values of NumericTextBox with an Entry control in Xamarin.Forms
2 mins read
SfNumericTextBox allows its properties to bind with other control’s properties. In this, we can bind the Value property of SfNumericTextBox into Text property of Entry control. Hence, we can show the changes of SfNumericTextBox’s Value will reflect in Entry as well as changes are in Entry control will reflect in SfNumericTextBox’s Value.
The following code example will explain how to work with NumericTextBox two way binding with Entry control.
Code in Xaml:
<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:d="http://xamarin.com/schemas/2014/forms/design" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Synfusion="clr-namespace:Syncfusion.SfNumericTextBox.XForms;assembly=Syncfusion.SfNumericTextBox.XForms" xmlns:local="clr-namespace:NumericTextBox_Binding" mc:Ignorable="d" x:Class="NumericTextBox_Binding.MainPage"> <ContentPage.Resources> <ResourceDictionary> <local:ObjectConverter x:Key="valueconverter" /> </ResourceDictionary> </ContentPage.Resources> <ContentPage.Content> <Grid BackgroundColor="#E4EAF0" Padding="0,30,0,0"> <Grid.RowDefinitions> <RowDefinition Height="30"/> <RowDefinition Height="50"/> <RowDefinition Height="30"/> <RowDefinition Height="50"/> </Grid.RowDefinitions> <Label x:Name="firstNumeric" Text="NumericTextBox" FontSize="20" HorizontalOptions="Center" FontAttributes="Bold" HorizontalTextAlignment="Start" VerticalTextAlignment="Start"/> <Synfusion:SfNumericTextBox Grid.Row="1" WidthRequest="180" HorizontalOptions="Center" AllowNull="true" BorderColor="Blue" TextColor="Maroon" x:Name="sfNumericTextBox" Value="{Binding ValueNumeric,Mode=TwoWay,Converter={StaticResource valueconverter}}" FormatString="p" ValueChangeMode="OnKeyFocus" PercentDisplayMode="Value" FontSize="20" /> <Label x:Name="secondNumeric" Grid.Row="2" Text="Entry" HorizontalOptions="Center" FontSize="20" FontAttributes="Bold" HorizontalTextAlignment="Start" VerticalTextAlignment="Start"/> <Entry Text="{Binding ValueNumeric,Mode=TwoWay}" Grid.Row="3" WidthRequest="180" HorizontalOptions="Center" FontSize="20"/> </Grid > </ContentPage.Content> </ContentPage>
Code in C#:
namespace NumericTextBox_Binding { [DesignTimeVisible(false)] public partial class MainPage : ContentPage { public MainPage() { InitializeComponent(); this.BindingContext = new ViewModel(); } } public class ObjectConverter : IValueConverter { // From Entry we get the string value before binding that string value into NumericTextBox,it has to convert to double public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return double.Parse(value.ToString()); } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return double.Parse(value.ToString()); } } //ViewModel class to have bindable property public class ViewModel : INotifyPropertyChanged { // Set and Get the "Value" property's bindable property private double valueNumeric = 1000; public double ValueNumeric { get { return valueNumeric; } set { valueNumeric = value; RaisePropertyChanged("ValueNumeric"); } } public void RaisePropertyChanged(string propertyname) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyname)); } } public event PropertyChangedEventHandler PropertyChanged; } }
Also please find the sample here for reference.