Hi Rod, try following demo.
XAML:
<Page
x:Class="App05.Page05"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App05"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Page.Resources>
<local:EnforceNumericConvert x:Key="EnforceNumericConvert"/>
<Style x:Key="CommonTextboxStyle" TargetType="TextBox">
<Setter Property="Background" Value="LightCoral"/>
</Style>
</Page.Resources>
<StackPanel>
<TextBox
x:Name="HourlyTextBox"
Style="{StaticResource CommonTextboxStyle}"
Text="{x:Bind ViewModel.Hourly, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource EnforceNumericConvert}}" />
<!-- TextBlock for testing Hourly value-->
<TextBlock Text="{x:Bind ViewModel.Hourly, Mode=TwoWay}"/>
</StackPanel>
</Page>
And code:
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
namespace App05
{
public sealed partial class Page05 : Page
{
public Page05() => this.InitializeComponent();
public ViewModel ViewModel { get; } = new ViewModel();
}
public class ViewModel : INotifyPropertyChanged
{
private Data salaryConvUS = new Data();
private float hourly;
public float Hourly
{
get => salaryConvUS.HourlyFloat;
set
{
Debug.WriteLine(value.ToString());
Set(ref hourly, value); //Template10 method
salaryConvUS.HourlyFloat = hourly;
OnPropertyChanged();
}
}
private void Set(ref float input, float value) => input = value;
public event PropertyChangedEventHandler PropertyChanged;
internal void OnPropertyChanged([CallerMemberName] string propName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public class Data : INotifyPropertyChanged
{
private float _hourlyFloat;
public float HourlyFloat
{
get => this._hourlyFloat;
set { this._hourlyFloat = value; OnPropertyChanged(); }
}
public event PropertyChangedEventHandler PropertyChanged;
internal void OnPropertyChanged([CallerMemberName] string propName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
public class EnforceNumericConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return value.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
float par = 0;
float.TryParse(value.ToString(), out par);
return par;
}
}
}