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

时间:2017-01-04 15:48:23

标签: wpf combobox textbox styles border

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

enter image description here

正如您在文本框中看到的那样,每边的边框厚度都不相同。 在文本框的情况下,例如在右侧更亮。 同样在组合框上,顶部和左侧都有阴影......

我怎么能解决这个问题,我只想在我的控件周围找到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,颜色设置为紫色,它看起来像这样:

enter image description here

所以伙计们,厚度也不好:1 px,例如厚度: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"/> 

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

2 个答案:

答案 0 :(得分:2)

尝试将SnapsToDevicePixels和/或UseLayoutRounding属性设置为True:

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

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

修改:将ClassicBorderDecorator替换为普通的Border元素:

<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>

答案 1 :(得分:0)

如果要自定义文本框和组合框边框,则需要通过右键单击控件并选择编辑模板来更改这些控件的默认样式和模板。