如何自定义文本框和组合框边框 WPF

How to customize textbox and combo box border WPF

在我 post 我的代码让我 post 图像之前,您可以轻松地注意到实际发生的事情:

正如您在文本框上看到的那样,每一边的边框粗细都不相同。 例如,在文本框的情况下,右侧要亮得多。 同样在组合框上,顶部和左侧有类似阴影的东西..

我该如何解决这个问题,我只想在我的控件周围使用 1px 蓝色边框..

这是我的代码:

<ComboBox Name="cmbComboBoxOne"  Height="40" BorderThickness="1"   VerticalAlignment="Center" BorderBrush="#0091EA" ></ComboBox>
<TextBox Name="txtTextBoxOne"    TextWrapping="Wrap"  Text="TextBox" BorderThickness="1"        BorderBrush="#0091EA" />

编辑:

我应用了编辑模板副本,我将边框粗细设置为 1,颜色设置为紫色,它看起来像这样:

所以伙计们,thickness:1 px 又不好,例如 thickness:2px 太棒了,所有边都是平等的,但 2px 对我来说太多了..

这是我编辑模板后的代码:

<TextBox x:Name="txtName" Grid.Column="1" SnapsToDevicePixels="True" UseLayoutRounding="True" Grid.Row="0" Margin="5,0,10,0" TextWrapping="Wrap" Text="TextBox" Height="40"  VerticalAlignment="Center" Style="{DynamicResource TextBoxStyle1}"  >
            <TextBox.Resources>
                <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
                    <Setter Property="BorderBrush" Value="Purple"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
                    <Setter Property="Padding" Value="1"/>
                    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
                    <Setter Property="HorizontalContentAlignment" Value="Left"/>
                    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
                    <Setter Property="AllowDrop" Value="true"/>
                    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
                    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type TextBox}">
                                <Microsoft_Windows_Themes:ClassicBorderDecorator x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" BorderStyle="Sunken" Background="{TemplateBinding Background}">
                                    <ScrollViewer x:Name="PART_ContentHost"/>
                                </Microsoft_Windows_Themes:ClassicBorderDecorator>
                                <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>
                        </Setter.Value>
                    </Setter>
                </Style>
            </TextBox.Resources>
        </TextBox>

如你们所见

<Setter Property="BorderBrush" Value="Purple"/>
                    <Setter Property="BorderThickness" Value="1"/> 

已设置,但结果在某种程度上是相同的:/

尝试将 SnapsToDevicePixels and/or UseLayoutRounding 属性设置为 True:

<ComboBox Name="cmbComboBoxOne" ... SnapsToDevicePixels="True" UseLayoutRounding="True" />

如果这不起作用,您可以尝试修改控件的控件模板。在 Visual Studio 2012+ 或 Blend 的设计模式中右键单击它们并选择编辑模板->编辑副本以将默认模板复制到您的 XAML 标记中,然后在边框元素上设置上述属性在生成的模板中。

编辑: 用普通的 Border 元素替换 ClassicBorderDecorator:

<TextBox x:Name="txtName" Grid.Column="1" SnapsToDevicePixels="True" UseLayoutRounding="True" Grid.Row="0" Margin="5,0,10,0" TextWrapping="Wrap" Text="TextBox" Height="40"  VerticalAlignment="Center" Style="{DynamicResource TextBoxStyle1}"  >
    <TextBox.Resources>
        <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
            <Setter Property="BorderBrush" Value="Purple"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}"/>
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="Padding" Value="1"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"
                                        Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                            <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>
                </Setter.Value>
            </Setter>
        </Style>
    </TextBox.Resources>
</TextBox>

如果您想自定义文本框和组合框边框,您需要更改这些控件的默认样式和模板,方法是右键单击控件并select编辑模板。