简体   繁体   English

使用Blend和WPF更改焦点文本框的边框样式

[英]Change the Border Style of a Focused Text Box using Blend and WPF

I've been headdesking over this for quite some time. 我已经为这个工作了很长时间了。 Can someone please explain to me, using Expression Blend, how to edit/override the default FocusVisualStyle for a text box? 有人可以使用Expression Blend向我解释如何编辑/覆盖文本框的默认FocusVisualStyle吗? Currently all of my text boxes have that blue accent when they're selected, and this doesn't match my current application's theme, but I just can't figure out how to override this. 当前,所有我的文本框在被选中时都带有蓝色的重音,这与我当前应用程序的主题不符,但是我只是想不出如何覆盖它。 I think I've set the Border brush in about 10 different places in various templates, events, and properties, and it just won't override. 我想我已经在各种模板,事件和属性的大约10个不同位置设置了“边框”笔刷,并且它不会被覆盖。

Edit: Buttons do this, as well, and it looks even more ugly. 编辑:按钮也这样做,而且看起来更加难看。 I'd like a solution that incorporates all controls that behave like this. 我想要一个包含所有行为类似的解决方案。

A set of directions for Blend would be preferable to just pasting XAML, but at this point I can't get picky. 对于Blend的一组指导要比仅粘贴XAML更好,但是在这一点上,我还是不挑剔。

The behavior you're seeing is actually part of the ListBoxChrome element that exists within TextBox's default control template. 您看到的行为实际上是TextBox的默认控件模板中存在的ListBoxChrome元素的一部分。 You could easily swap this out with a simpler template that uses the BorderBrush property, which is what Silverlight's TextBox does. 您可以轻松地将其替换为使用BorderBrush属性的更简单的模板,这是Silverlight的TextBox所做的。

<ControlTemplate TargetType="{x:Type TextBox}">
    <Border x:Name="Bd" 
            Background="{TemplateBinding Background}" 
            BorderBrush="{TemplateBinding BorderBrush}"
            BorderThickness="{TemplateBinding BorderThickness}"
            CornerRadius="2">
        <ScrollViewer x:Name="PART_ContentHost" />
    </Border>
    <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>

To add to the answer, for Blend, you can right-click the Control to define a new template. 要添加到答案中,对于Blend,您可以右键单击控件以定义新模板。 Once you do that, in the XAML view, replace the generated <ControlTemplate> with the one @Josh Einstein provided. 完成后,在XAML视图中,将生成的<ControlTemplate>替换为提供的一个@Josh Einstein。 I also added SnapToDevicePixels onto the border so it wouldn't anti-alias, but this is optional. 我还将SnapToDevicePixels添加到边框上,因此它不会抗锯齿,但这是可选的。 After you do this, to change the Text Box's border or background color, use the Template. 完成此操作后,要更改文本框的边框或背景颜色,请使用模板。 But to change the text's foreground color, use the object instance instead (not the template). 但是要更改文本的前景色,请改用对象实例(而不是模板)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM