WPF TextBox丑陋的边框问题

PaN*_*1Me 4 wpf textbox border

我想覆盖WPF中的默认TextBox边框.我有这种风格适用于所有TextBoxes.

<!-- StyleTextBox-->
<Style x:Key="StyleTextBox" TargetType="{x:Type TextBox}">
    <Setter Property="MinHeight" Value="20" />
    <Setter Property="HorizontalAlignment" Value="Left"/>
    <Setter Property="Margin" Value="3"/>
    <Setter Property="IsEnabled" Value="{DynamicResource WriteAble}"/>
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="VerticalContentAlignment" Value="Top" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="BorderBrush" Value="{StaticResource ButtonFont_DarkGray}" />
    <Style.Triggers>
        <!--Resolves multiline textbox vertical alignment problem-->
        <Trigger Property="TextWrapping" Value="NoWrap">
            <Setter Property="VerticalContentAlignment" Value="Center" />
        </Trigger>
    </Style.Triggers>
</Style> 
Run Code Online (Sandbox Code Playgroud)

我添加SnapsToDevicePixels="True"了在LCD显示器上正确显示边框.

但是,每个TextBox似乎都不同.一些边界丢失或灰色..有谁知道为什么?

Sor*_*oot 7

您可以尝试编辑文本框的模板,并将边框名称Bd更改为"真实"边框而不是铬边框.像这样:

<ControlTemplate x:Key="TextBoxBaseControlTemplate1" 
          TargetType="{x:Type TextBoxBase}">
  <Border x:Name="Bd" SnapsToDevicePixels="True" 
          Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}"
          BorderThickness="{TemplateBinding BorderThickness}" >
    <ScrollViewer x:Name="PART_ContentHost" 
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
  </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>
Run Code Online (Sandbox Code Playgroud)

将此setter添加到您的样式以启用模板:

<Setter Property="Template" 
        Value="{DynamicResource TextBoxBaseControlTemplate1}"/>  
Run Code Online (Sandbox Code Playgroud)