TLDR: my textboxes will update their Foreground and Background colors properly when manually selected by touch input, but will fail to update if I use the Textbox.Focus() method after they are created.
The question is similar to this - windows phone - textbox background
My Application creates a textbox with an associated GotFocus event, that changes the Foreground and Background colors of the textbox to the system default whenever the textbox receives focus. Upon the user pressing the enter key on the keypad, the app then generates another identical textbox below the first.
The issue is that for any of these textboxes, if the textbox is pressed manually, everything works fine, and the textbox is displayed how it's meant to be. However, if I use TextBox.Focus() after the enter key is pressed, although focus is passed to the textbox, and the GotFocus event has been processed, the Background and Foreground colors are not updated, leaving white text on a white background.
I have tried passing focus between the textbox for a number of times (up to 10), and even though I can confirm that the focus is being passed as it should, the colors are only updated if the user gives focus to the textbox (if I give focus to the textbox via code, I must then manually deselect and then reselect it for the color change to apply. If I don't give focus via code I can simply select it).
The code for this is:
public void txtInputKeyUp(object sender, KeyEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if (e.Key == Key.Enter)
{
Evaluate(txtBox);
InitializeDivider();
InitializeTextInput();
InitializeTextOutput();
txtInput[txtInput.Length - 1].Focus();
}
}
public void txtInputGotFocus(object sender, RoutedEventArgs e)
{
TextBox txtBox = (TextBox)sender;
if(txtBox.Text == "Input Here")
{
txtBox.Text = "";
}
txtBox.Foreground = (SolidColorBrush)App.Current.Resources["PhoneForegroundBrush"];
txtBox.Background = (SolidColorBrush)App.Current.Resources["PhoneBackgroundBrush"];
}
The Initializeblabla stuff basically just creates the relevant textboxes and all associated data. I've tried switching focus between textboxes, disabling and enabling the specified textboxes and a few other options, and consistently the only thing that works is not giving focus to the textbox via code, but rather waiting for the user to select the textbox, something I'm not really satisfied with. Attempting to manually edit the style didn't help either.