为什么在WPF中坚持使用TextBox Border Color而不更改?

丹尼·布朗

据我了解,当焦点对准文本框时,我应该使用样式触发器来更新文本框的边框颜色。但是,无论我做什么,它总是变成系统默认的蓝色,而不是我指定的黑色。

谁有想法?

代码如下:

<UserControl.Resources>
    <Style TargetType="TextBox">
        <Style.Triggers>
            <Trigger Property="IsFocused" Value="True">
                <Setter Property="BorderBrush" Value="Black" />
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Resources>
阿纳托利·尼古拉耶夫(Anatoliy Nikolaev)

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

<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 Seven上测试。

Edit: why is this happening?

TextBox在Windows 7下的Blend中使用了默认样式,这里是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并且可能在BorderThickness上存在条件BorderBrush如果它们remove / reset是蓝色的,则“渐变边框”将消失,并且不需要将值设置为BorderThickness大于1。

ILSpy我发现ChangeVisualState(bool)在TextBoxBase类的方法,那就是:

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);
}

事实证明,这些视觉状态是“系统地”实现的,并且没有样式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么在WPF中坚持使用TextBox Border Color而不进行更改?

来自分类Dev

为什么CSS border-color继承了color属性?

来自分类Dev

为什么当border-bottom-style等于'none'时指定border-bottom-color?

来自分类Dev

为什么-fx-border-color重置TextField的边框半径?

来自分类Dev

使用border-collapse更改表格的宽度:

来自分类Dev

Double border with different color

来自分类Dev

How to change QTableView border color?

来自分类Dev

回答问题后,如何使用[style.border]更改边框样式?

来自分类Dev

iOS: Rounded rectangle with border bleeds color

来自分类Dev

border-color失败,继承值

来自分类Dev

border-color失败,继承值

来自分类Dev

jQuery .height()无法与框大小配合使用:border-border

来自分类Dev

如果使用sudo,为什么$ HOME不更改?

来自分类Dev

为什么在使用AJAX时不更改$ _SESSION变量(PHP)?

来自分类Dev

为什么plm不更改方法?(右)

来自分类Dev

.vimrc为什么不更改vimtutor的外观?

来自分类Dev

猫为什么不更改访问时间?

来自分类Dev

为什么不更改标签的颜色?

来自分类Dev

为什么border-image-width在border-image形式语法中出现两次?

来自分类Dev

WPF Border ZIndex无法正常工作

来自分类Dev

WPF Border ZIndex无法正常工作

来自分类Dev

为什么不更改数据时会更改数据?

来自分类Dev

如何在vuetify中更改v-data-table标头的backkground-color和border-radius?

来自分类Dev

使用CSS在Border中绘制曲线

来自分类Dev

使用 flex 和 border-box 等宽

来自分类Dev

如何在悬停时不悬停CSS Img Border的图像

来自分类Dev

如何从jQuery中获取内联样式的border-color?

来自分类Dev

jQuery $(this).css('border-color') 返回旧的边框颜色

来自分类Dev

'int border = borderType & ~BORDER_ISOLATED' 变量是什么意思?

Related 相关文章

  1. 1

    为什么在WPF中坚持使用TextBox Border Color而不进行更改?

  2. 2

    为什么CSS border-color继承了color属性?

  3. 3

    为什么当border-bottom-style等于'none'时指定border-bottom-color?

  4. 4

    为什么-fx-border-color重置TextField的边框半径?

  5. 5

    使用border-collapse更改表格的宽度:

  6. 6

    Double border with different color

  7. 7

    How to change QTableView border color?

  8. 8

    回答问题后,如何使用[style.border]更改边框样式?

  9. 9

    iOS: Rounded rectangle with border bleeds color

  10. 10

    border-color失败,继承值

  11. 11

    border-color失败,继承值

  12. 12

    jQuery .height()无法与框大小配合使用:border-border

  13. 13

    如果使用sudo,为什么$ HOME不更改?

  14. 14

    为什么在使用AJAX时不更改$ _SESSION变量(PHP)?

  15. 15

    为什么plm不更改方法?(右)

  16. 16

    .vimrc为什么不更改vimtutor的外观?

  17. 17

    猫为什么不更改访问时间?

  18. 18

    为什么不更改标签的颜色?

  19. 19

    为什么border-image-width在border-image形式语法中出现两次?

  20. 20

    WPF Border ZIndex无法正常工作

  21. 21

    WPF Border ZIndex无法正常工作

  22. 22

    为什么不更改数据时会更改数据?

  23. 23

    如何在vuetify中更改v-data-table标头的backkground-color和border-radius?

  24. 24

    使用CSS在Border中绘制曲线

  25. 25

    使用 flex 和 border-box 等宽

  26. 26

    如何在悬停时不悬停CSS Img Border的图像

  27. 27

    如何从jQuery中获取内联样式的border-color?

  28. 28

    jQuery $(this).css('border-color') 返回旧的边框颜色

  29. 29

    'int border = borderType & ~BORDER_ISOLATED' 变量是什么意思?

热门标签

归档