GitHub URL : https://github.com/mistrypragnesh40/CurrencyControl
CurrencyControl.Xaml
<?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Name="this" x:Class="CurrencyControl.CustomControl.CurrencyControl"> <ContentView.Content> <Grid> <Entry FontSize="20" Keyboard="Numeric" Text="{Binding Source={x:Reference this},Path=Amount}" x:Name="txtAmmount" Unfocused="txtAmmount_Unfocused" IsVisible="False" Grid.Row="0" > </Entry> <Label FontSize="20" x:Name="lblDisplayAmount" IsVisible="True" Grid.Row="0" > <Label.GestureRecognizers> <TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped_ForDisplayTextBox" /> </Label.GestureRecognizers> </Label> </Grid> </ContentView.Content> </ContentView>
CurrencyControl.Xaml.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Xamarin.Forms; using Xamarin.Forms.Xaml; namespace CurrencyControl.CustomControl { [XamlCompilation(XamlCompilationOptions.Compile)] public partial class CurrencyControl : ContentView { public CurrencyControl() { InitializeComponent(); } private void TapGestureRecognizer_Tapped_ForDisplayTextBox(object sender, EventArgs e) { lblDisplayAmount.IsVisible = false; txtAmmount.IsVisible = true; txtAmmount.Focus(); } private void txtAmmount_Unfocused(object sender, FocusEventArgs e) { txtAmmount.IsVisible = false; lblDisplayAmount.IsVisible = true; } #region Amount Property public static readonly BindableProperty AmountProperty = BindableProperty.Create( propertyName: nameof(Amount), returnType: typeof(double), declaringType: typeof(CurrencyControl), defaultValue: 0d, defaultBindingMode: BindingMode.TwoWay, propertyChanged: AmountPropertyChanged); private static void AmountPropertyChanged(BindableObject bindable, object oldValue, object newValue) { var control = (CurrencyControl)bindable; if (newValue != null) { double val = Convert.ToDouble(newValue); if (val > 0) { control.lblDisplayAmount.Text = "$" + val.ToString("#,##0.00"); }; } } public double Amount { get { return (double)base.GetValue(AmountProperty); } set { base.SetValue(AmountProperty, value); } } #endregion #region Place Holder Property public static readonly BindableProperty PlaceHolderProperty = BindableProperty.Create( propertyName: nameof(PlaceHolder), returnType: typeof(string), declaringType: typeof(CurrencyControl), defaultValue: "", defaultBindingMode: BindingMode.TwoWay, propertyChanged: PlaceHolderPropertyChanged); private static void PlaceHolderPropertyChanged(BindableObject bindable, object oldValue, object newValue) { var control = (CurrencyControl)bindable; if (newValue != null) { string val = (string)newValue; if (!string.IsNullOrWhiteSpace(val)) { control.txtAmmount.Placeholder = val; }; } } public string PlaceHolder { get { return (string)base.GetValue(PlaceHolderProperty); } set { base.SetValue(PlaceHolderProperty, value); } } #endregion } }
Implementation
MainPage.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:customcontrol="clr-namespace:CurrencyControl.CustomControl" x:Class="CurrencyControl.MainPage"> <StackLayout Margin="10"> <Frame BackgroundColor="#2196F3" Padding="24" CornerRadius="0"> <Label Text="Welcome to Xamarin.Forms!" HorizontalTextAlignment="Center" TextColor="White" FontSize="36"/> </Frame> <Label Text="Currency Control Demo" FontSize="Title" Padding="30,10,30,10"/> <customcontrol:CurrencyControl Amount="5000" PlaceHolder="Aamount" /> </StackLayout> </ContentPage>
No comments:
Post a Comment