2

I'm working on a simple UWP app. I want to make it possible for the user to enter some monetary value, in this case an hourly rate. The data type is decimal. Here's the property from the viewmodel:

private decimal hourly;
public decimal Hourly
{
get => salaryConvUS.Hourly;
set
    {
       Set(ref hourly, value); //Template10 method
       salaryConvUS.Hourly = hourly;
    }
}

And here's the XAML code:

<TextBox x:Name="HourlyTextBox" 
     Text="{x:Bind ViewModel.Hourly, Mode=TwoWay}"
     Style="{StaticResource CommonTextboxStyle}" />

It would seem to me to be pretty straight forward, but I'm getting an error that says,

App.InitializeComponent.AnonymousMethod__3_0(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e)"

in the App.g.i.cs file. I've searched for this error, but what I found didn't apply to this situation.

The one thing that might have something to do with it is a problem I know that Windows 8 applications had, in that you couldn't bind something like a textbox to a decimal data type. You had to do some sort of conversion. Is that what's going on here in UWP?

halfer
  • 19,824
  • 17
  • 99
  • 186
Rod
  • 4,107
  • 12
  • 57
  • 81

1 Answers1

4

TextBlock holds a string value. So you can't bind decimal value with a TextBlock directly.

Use the ToString() method to bind correctly.

You can use something like this -

private string hourly;
public string Hourly
{
    //Your Algorithm
}
Mahmudul Hasan
  • 798
  • 11
  • 35
  • 1
    Thank you, Mahmudal. Although I do think the issue is with the decimal data type. UWP binding doesn't seem to like it. I did try changing the Hourly to float - that worked. But your solution would also work. – Rod Apr 20 '19 at 23:54
  • 1
    In my machine, the tooltip says that I cannot assign a decimal value to a string container. So I thought that it could be a casting problem. Anyways, glad to know that it works! – Mahmudul Hasan Apr 21 '19 at 06:10