Why does TextBox Border Colour insist on and not changing in WPF?

后端 未结 2 1952
既然无缘
既然无缘 2021-01-12 13:08

As far as I understand I should be using Style triggers to update the TextBox\'s border colour when it is focused. However no matter what I do it always turns to the system

2条回答
  •  温柔的废话
    2021-01-12 13:46

    Try set for BorderThickness value more than 1 (by default):

    
        
    
    
    
        
    
    

    Tested on Windows Seven.

    Edit: why is this happening?

    I looked in the default style for TextBox in Blend under Windows 7, here it is ControlTemplate:

    
        
            
        
    
        
            
                
                
            
        
    
    

    Here there is two parameters:

    RenderMouseOver="{TemplateBinding IsMouseOver}"
    RenderFocused="{TemplateBinding IsKeyboardFocusWithin}"
    

    They are responsible for blue gradient Border when states Focus and MouseOver and probably there stands a condition on BorderThickness and BorderBrush. If they remove / reset the blue gradient Border will disappear and will not need to set values for BorderThickness greater than 1.

    In ILSpy I found ChangeVisualState(bool) method in TextBoxBase class, here it is:

    internal override void ChangeVisualState(bool useTransitions)
    {
        if (!base.IsEnabled)
        {
            VisualStateManager.GoToState(this, "Disabled", useTransitions);
        }
        else
        {
            if (this.IsReadOnly)
            {
                VisualStateManager.GoToState(this, "ReadOnly", useTransitions);
            }
            else
            {
                if (base.IsMouseOver)
                {
                    VisualStateManager.GoToState(this, "MouseOver", useTransitions);
                }
                else
                {
                    VisualStateManager.GoToState(this, "Normal", useTransitions);
                }
            }
        }
    
        if (base.IsKeyboardFocused)
        {
            VisualStateManager.GoToState(this, "Focused", useTransitions);
        }
        else
        {
            VisualStateManager.GoToState(this, "Unfocused", useTransitions);
        }
    
        base.ChangeVisualState(useTransitions);
    }
    

    It turns out that these visual states implemented "systematically" and in Styles not present.

提交回复
热议问题