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

丹尼·布朗

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

有人有想法么?

代码如下:

<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 7上进行了测试。

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 条评论
登录 后参与评论

相关文章

WPF绑定不进行任何更改

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

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

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

为什么不进行交换?

为什么不进行引导输入微调器的更改事件?

Angular组件视图层不使用localStorage更改而无需更新而不进行刷新

如何在Windows中锁定文件而不进行更改或使用第三方工具?

为什么文件全局不进行分词?

GEKKO为什么不进行初始测量?

为什么不进行比较就返回假?

如何先使用代码在不进行迁移的情况下在实体框架中应用数据库架构更改

使用命令行Ubuntu在文件中更改Particualy字符串值而不进行字符串比较

什么是泛型泛型?它们如何解决“类型擦除”问题?为什么不进行重大更改就不能添加它们?

请告知错误是什么...为什么不进行编译?

为什么简单地打开并保存文件(不进行任何更改)会导致该文件在MySQL LOAD操作中变得不可读?

WebSockets ping / pong,为什么不进行TCP keepalive?

为什么此@Transactional方法不进行多次保存

为什么这里不进行隐式转换?

为什么不进行屏幕外更新的UICollectionViewCells?

为什么不进行这种简单的Morte程序类型检查?

为什么在选择模型之前不进行模型调整?

为什么在我的情况下不进行整数提升?

为什么要从多个表中选择而不进行联接

为什么不进行四舍五入(JavaScript)?

为什么lodash deepClone不进行Function属性的深层克隆?

单击图标后为什么不进行模式启动?

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

在React中使用脚本而不进行捆绑