We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to bind the values of NumericTextBox with an Entry control in Xamarin.Forms

Platform: Xamarin.Forms |
Control: SfNumericTextBox |
Published Date: March 23, 2017 |
Last Revised Date: June 30, 2020

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

 

SfNumericTextBox Two way Binding

 

Also please find the sample here for reference.

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile