Search code examples
c#uwptemplate10

The data I enter into a TextBox is not being assigned to the value variable in a UWP app


I'm working on a simple UWP app, using Template 10. I want to enter monetary data into a TextBox. It's my understanding that I should use a string variable in the View-Model. So, for the moment I'm just making sure that the data I enter, when running the app, actually works. But it doesn't. When running or debugging it, and if I enter something like "10" (without the double quotes), what the variable value is assigned is "0". Which doesn't make sense to me. Here's the XAML:

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

And here's the code from the View-Model:

private string hourly;
public string Hourly
{
    get => hourly;
    set
    {
         _ = Set(ref hourly, value);
    }
}

Here's the Code-behind code:

using Windows.UI.Xaml.Controls;
using SalaryConv;

namespace SalaryConversion.Views
{
    public sealed partial class MainPage : Page
    {
        private SalaryUnitsEnum lastHadFocus;

        public MainPage()
        {
            InitializeComponent();
            NavigationCacheMode = 
Windows.UI.Xaml.Navigation.NavigationCacheMode.Enabled;
        }


        #region GettingFocus events

        private void HourlyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Hourly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Hourly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void WeeklyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Weekly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Weekly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void BiWeeklyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.BiWeekly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.BiWeekly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void SemiMonthlyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.SemiMonthly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.SemiMonthly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void MonthlyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Monthly)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Monthly;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        private void AnnuallyTextBox_GettingFocus(Windows.UI.Xaml.UIElement sender, Windows.UI.Xaml.Input.GettingFocusEventArgs args)
        {
            if (lastHadFocus == SalaryUnitsEnum.Annually)
            {
                return;
            }

            lastHadFocus = SalaryUnitsEnum.Annually;

            ClearOtherMonetaryTextboxes(lastHadFocus);
        }

        #endregion

        #region ClearOtherMonetaryTextboxes helper method

        private void ClearOtherMonetaryTextboxes(SalaryUnitsEnum lastHadFocus)
        {
            if (lastHadFocus != SalaryUnitsEnum.Hourly)
            {
                HourlyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.Weekly)
            {
                WeeklyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.BiWeekly)
            {
                BiWeeklyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.SemiMonthly)
            {
                SemiMonthlyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.Monthly)
            {
                MonthlyTextBox.Text = "0";
            }

            if (lastHadFocus != SalaryUnitsEnum.Annually)
            {
                AnnuallyTextBox.Text = "0";
            }
        }

        #endregion
    }
}

Solution

  • Thanks to Richard Zhang's suggestion of looking at my code-behind, I discovered there that I had previously written some code to handle the controls on the screen. It was this code which was resetting the values to 0 (indirectly). I had written that code a while ago, so I'd forgotten all about it.

    Thank you, Richard, for making that suggestion. It helped me see what I had done and after reviewing it I was able to easily resolve it.