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. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to work with NumericTextBox two way binding with an Entry control

Platform: Xamarin.Forms |
Control: SfNumericTextBox |
Published Date: March 23, 2017 |
Last Revised Date: July 4, 2019

SfNumericTextBox allows its properties to bind with other control’s properties. In this, we can bind the Value properties of SfNumericTextBox into Text property of Entry control. Hence, we can show the changes of numericTextBox’s Value will reflect in Entry as well as changes are in Entry control will reflect in numericTextBox’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:local="clr-namespace:KBSolution"
    xmlns:Synfusion="clr-namespace:Syncfusion.SfNumericTextBox.XForms;assembly=Syncfusion.SfNumericTextBox.XForms"
    x:Class="KBSolution.KBSolutionPage">
 
<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 KBSolution
{
public partial class KBSolutionPage : ContentPage
{
    public KBSolutionPage()
    {
        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

 

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