Change WPF TextBox border when it has focus
-
I am trying to change the border color of a WPF TextBox when it has focus. I'm doing this in code using the focus events: private void TextBox_GotFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Red; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Blue; } This does not work. My googling seemed to indicate that the TextBox has built in focus behavior that could be overriding my behavior. The proposed solution was to null the "TextBox.FocusVisualStyle' property. This also did not work. Any ideas out there? Aaron
-
I am trying to change the border color of a WPF TextBox when it has focus. I'm doing this in code using the focus events: private void TextBox_GotFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Red; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Blue; } This does not work. My googling seemed to indicate that the TextBox has built in focus behavior that could be overriding my behavior. The proposed solution was to null the "TextBox.FocusVisualStyle' property. This also did not work. Any ideas out there? Aaron
You would need to modify the control template. The border shenanigans are done in there. The ListBox, TextBox, etc. all use a "chrome border" which does all that using triggers.
-
I am trying to change the border color of a WPF TextBox when it has focus. I'm doing this in code using the focus events: private void TextBox_GotFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Red; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Blue; } This does not work. My googling seemed to indicate that the TextBox has built in focus behavior that could be overriding my behavior. The proposed solution was to null the "TextBox.FocusVisualStyle' property. This also did not work. Any ideas out there? Aaron
I have try but not success My code is
<Style.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="BorderBrush" Value="Red"></Setter> <Setter Property="BorderThickness" Value="4" /> </Trigger> </Style.Triggers> Hello
Life's Like a mirror. Smile at it & it smiles back at you.- P Pilgrim So Smile Please
-
I am trying to change the border color of a WPF TextBox when it has focus. I'm doing this in code using the focus events: private void TextBox_GotFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Red; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Blue; } This does not work. My googling seemed to indicate that the TextBox has built in focus behavior that could be overriding my behavior. The proposed solution was to null the "TextBox.FocusVisualStyle' property. This also did not work. Any ideas out there? Aaron
In the xaml of the window, do this: <Window.Resources> <Style x:Key="TextBoxStyle" TargetType="TextBox"> <Style.Triggers> <Trigger Property="IsFocused" Value="False"> <Setter Property="BorderBrush" Value="Blue"/> </Trigger> <Trigger Property="IsFocused" Value="True"> <Setter Property="BorderBrush" Value="Red"/> </Trigger> </Style.Triggers> </Style> </Window.Resources>
-
I am trying to change the border color of a WPF TextBox when it has focus. I'm doing this in code using the focus events: private void TextBox_GotFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Red; } private void TextBox_LostFocus(object sender, RoutedEventArgs e) { _text.BorderBrush = Brushes.Blue; } This does not work. My googling seemed to indicate that the TextBox has built in focus behavior that could be overriding my behavior. The proposed solution was to null the "TextBox.FocusVisualStyle' property. This also did not work. Any ideas out there? Aaron
This answer will upset the wpf-purists out there, but when time is money, try this: Instead of creating a ControlTemplate-Trigger nightmare, reduce the TextBox size with Margin="1" and BorderThickness="0". THEN, place the TextBox inside of Grid tags and place a in the Grid above TextBox. Example (with binding examples):
This code can be injected into a DataTemplate for a GridView or simply in a Window. Adding triggers to a simplified structure will prove easier than trying to override animations dependent on .Net version and operating system. I hate seeing my controls looking different on an XP system vs Win7, so this helps keep my projects looking clean on any display. Duct-tape solutions aren't always that bad!