latest posts

Something I never understood why it wasn't part of the base TextBox control is the ability to override the GotFocus colors with an easy to use property, like FocusForeground or FocusBackground. You could do something like this: [csharp] private void txtBxPlayerName_GotFocus(object sender, RoutedEventArgs e) { Foreground = new SolidColorBrush(Colors.Black); } [/csharp] By default, the Background on a Focus event of a Windows Phone TextBox control sets it to White, so if you had White Text previously, upon entering Text, your Text would be invisible. The workaround above isn't pretty, nor handles the LostFocus property (ie to revert back to your normal colors after entering Text. If you have 2,3,4 or more TextBox controls, this could get tedious/clutter up your code. My "fix", extending the TextBox Control: [csharp] using System; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace WPGame { public class jcTextBox : TextBox { #region New Background and Foreground Properties public static readonly DependencyProperty FocusForegroundProperty = DependencyProperty.Register("FocusForeground", typeof(Brush), typeof(jcTextBox), null); public Brush FocusForeground { get { return base.GetValue(FocusForegroundProperty) as Brush; } set { base.SetValue(FocusForegroundProperty, value); } } public static readonly DependencyProperty FocusBackgroundProperty = DependencyProperty.Register("FocusBackground", typeof(Brush), typeof(jcTextBox), null); public Brush FocusBackground { get { return base.GetValue(FocusBackgroundProperty) as Brush; } set { base.SetValue(FocusBackgroundProperty, value); } } public static readonly DependencyProperty BaseForegroundProperty = DependencyProperty.Register("BaseForeground", typeof(Brush), typeof(jcTextBox), null); public Brush BaseForeground { get { return base.GetValue(BaseForegroundProperty) as Brush; } set { base.SetValue(BaseForegroundProperty, value); } } public static readonly DependencyProperty BaseBackgroundProperty = DependencyProperty.Register("BaseBackground", typeof(Brush), typeof(jcTextBox), null); public Brush BaseBackground { get { return base.GetValue(BaseBackgroundProperty) as Brush; } set { base.SetValue(BaseBackgroundProperty, value); } } #endregion public jcTextBox() { BaseForeground = Foreground; BaseBackground = Background; } #region Focus Event Overrides protected override void OnGotFocus(RoutedEventArgs e) { Foreground = FocusForeground; Background = FocusBackground; base.OnGotFocus(e); } protected override void OnLostFocus(RoutedEventArgs e) { Foreground = BaseForeground; Background = BaseBackground; base.OnLostFocus(e); } #endregion } } [/csharp] To use it, add it to your project and then you can just drag and drop the control from the Toolbox and use it like so: [csharp] <my:jcTextBox x:Name="txtBxPlayerName" Background="#1e1e1e" Foreground="White" BaseBackground="#1e1e1e" BaseForeground="White" FocusForeground="Black" FocusBackground="#2e2e2e" AcceptsReturn="False" /> [/csharp] The output: [caption id="attachment_1354" align="aligncenter" width="180"] Unfocused jcTextBox Controls[/caption] [caption id="attachment_1355" align="aligncenter" width="180"] Visible Text with jcTextBox on Focus[/caption] [caption id="attachment_1356" align="aligncenter" width="180"] LostFocus jcTextBox Text with original colors[/caption] So there you have it, a "fix" for a common problem (at least in my experience). Feel free to use, rewrite, whatever with the jcTextBox class.