Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Change WPF TextBox border when it has focus

Change WPF TextBox border when it has focus

Scheduled Pinned Locked Moved WPF
csharpwpfquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astibich2
    wrote on last edited by
    #1

    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

    S K T S 4 Replies Last reply
    0
    • A astibich2

      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

      S Offline
      S Offline
      SledgeHammer01
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • A astibich2

        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

        K Offline
        K Offline
        Khaniya
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • A astibich2

          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

          T Offline
          T Offline
          Tarun K S
          wrote on last edited by
          #4

          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>

          1 Reply Last reply
          0
          • A astibich2

            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

            S Offline
            S Offline
            scottytk
            wrote on last edited by
            #5

            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!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups