1. <legend id='1DKD5'><style id='1DKD5'><dir id='1DKD5'><q id='1DKD5'></q></dir></style></legend>
    <tfoot id='1DKD5'></tfoot>

  2. <small id='1DKD5'></small><noframes id='1DKD5'>

    • <bdo id='1DKD5'></bdo><ul id='1DKD5'></ul>

      <i id='1DKD5'><tr id='1DKD5'><dt id='1DKD5'><q id='1DKD5'><span id='1DKD5'><b id='1DKD5'><form id='1DKD5'><ins id='1DKD5'></ins><ul id='1DKD5'></ul><sub id='1DKD5'></sub></form><legend id='1DKD5'></legend><bdo id='1DKD5'><pre id='1DKD5'><center id='1DKD5'></center></pre></bdo></b><th id='1DKD5'></th></span></q></dt></tr></i><div id='1DKD5'><tfoot id='1DKD5'></tfoot><dl id='1DKD5'><fieldset id='1DKD5'></fieldset></dl></div>
    1. 为什么在WPF中TextBox Border Color坚持而不改变?

      Why does TextBox Border Colour insist on and not changing in WPF?(为什么在WPF中TextBox Border Color坚持而不改变?)
      <tfoot id='Be2wY'></tfoot>

      <small id='Be2wY'></small><noframes id='Be2wY'>

        • <bdo id='Be2wY'></bdo><ul id='Be2wY'></ul>

            <legend id='Be2wY'><style id='Be2wY'><dir id='Be2wY'><q id='Be2wY'></q></dir></style></legend>
                <tbody id='Be2wY'></tbody>

                <i id='Be2wY'><tr id='Be2wY'><dt id='Be2wY'><q id='Be2wY'><span id='Be2wY'><b id='Be2wY'><form id='Be2wY'><ins id='Be2wY'></ins><ul id='Be2wY'></ul><sub id='Be2wY'></sub></form><legend id='Be2wY'></legend><bdo id='Be2wY'><pre id='Be2wY'><center id='Be2wY'></center></pre></bdo></b><th id='Be2wY'></th></span></q></dt></tr></i><div id='Be2wY'><tfoot id='Be2wY'></tfoot><dl id='Be2wY'><fieldset id='Be2wY'></fieldset></dl></div>
              • 本文介绍了为什么在WPF中TextBox Border Color坚持而不改变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                据我了解,我应该使用样式触发器来更新 TextBox 的边框颜色.但是无论我做什么,它总是变成系统默认的蓝色,而不是我指定的黑色.

                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 default blue, not the black I have specified.

                有人有什么想法吗?

                代码如下:

                <UserControl.Resources>
                    <Style TargetType="TextBox">
                        <Style.Triggers>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" Value="Black" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </UserControl.Resources>
                

                推荐答案

                尝试设置BorderThickness值大于1(默认):

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

                <Window.Resources>
                    <Style TargetType="{x:Type TextBox}">
                        <Style.Triggers>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter Property="BorderBrush" Value="Pink" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </Window.Resources>
                
                <Grid>
                    <TextBox Width="100"
                             Height="30"
                             Text="Test" 
                             BorderThickness="4" />
                </Grid>
                

                在 Windows 7 上测试.

                Tested on Windows Seven.

                为什么会这样?

                我在 Windows 7 下的 Blend 中查看了 TextBox 的默认样式,这里是 ControlTemplate:

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

                <ControlTemplate x:Key="TextBoxControlTemplate1" TargetType="{x:Type TextBox}">
                    <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
                        <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Microsoft_Windows_Themes:ListBoxChrome>
                
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
                

                这里有两个参数:

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

                当状态 FocusMouseOver 时,它们负责蓝色渐变边框,并且可能在 BorderThicknessBorderBrush<上存在条件/代码>.如果他们remove/reset蓝色渐变边框将消失,并且不需要将 BorderThickness 的值设置为大于 1.

                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.

                ILSpy 我在 TextBoxBase 类中找到了 ChangeVisualState(bool) 方法,这里是:

                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.

                这篇关于为什么在WPF中TextBox Border Color坚持而不改变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to keep the Text of a Read only TextBox after PostBack()?(PostBack()之后如何保留只读文本框的文本?)
                Winforms Textbox - Using Ctrl-Backspace to Delete Whole Word(Winforms 文本框 - 使用 Ctrl-Backspace 删除整个单词)
                C# - Add button click events using code(C# - 使用代码添加按钮单击事件)
                Multi-color TextBox C#(多色文本框 C#)
                How can i set the caret position to a specific index in passwordbox in WPF(如何将插入符号位置设置为 WPF 密码框中的特定索引)
                C# Numeric Only TextBox Control(C# 纯数字文本框控件)
                <tfoot id='MVmyE'></tfoot>
                    <legend id='MVmyE'><style id='MVmyE'><dir id='MVmyE'><q id='MVmyE'></q></dir></style></legend>
                  1. <i id='MVmyE'><tr id='MVmyE'><dt id='MVmyE'><q id='MVmyE'><span id='MVmyE'><b id='MVmyE'><form id='MVmyE'><ins id='MVmyE'></ins><ul id='MVmyE'></ul><sub id='MVmyE'></sub></form><legend id='MVmyE'></legend><bdo id='MVmyE'><pre id='MVmyE'><center id='MVmyE'></center></pre></bdo></b><th id='MVmyE'></th></span></q></dt></tr></i><div id='MVmyE'><tfoot id='MVmyE'></tfoot><dl id='MVmyE'><fieldset id='MVmyE'></fieldset></dl></div>
                    • <bdo id='MVmyE'></bdo><ul id='MVmyE'></ul>
                      • <small id='MVmyE'></small><noframes id='MVmyE'>

                            <tbody id='MVmyE'></tbody>