首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何自定义文本框和组合框边框WPF

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

Stack Overflow用户
提问于 2017-01-04 15:48:23
回答 2查看 1.2K关注 0票数 0

在我发布代码之前,让我发布图像,您可以很容易地注意到实际发生了什么:

正如您在文本框中所看到的,边框的厚度并不是每个边都一样。例如,在文本框中,右边要亮得多。在组合框上也有类似阴影的东西在上面和左边。

我怎样才能解决这个问题,我只想让1 1px蓝色边框围绕着我的控件。

这是我的密码:

代码语言:javascript
运行
复制
<ComboBox Name="cmbComboBoxOne"  Height="40" BorderThickness="1"   VerticalAlignment="Center" BorderBrush="#0091EA" ></ComboBox>
<TextBox Name="txtTextBoxOne"    TextWrapping="Wrap"  Text="TextBox" BorderThickness="1"        BorderBrush="#0091EA" />

编辑:

我应用了编辑模板复制,我将边框厚度设置为1,颜色设置为紫色,其外观如下:

所以伙计们,再一次它的厚度不好:1 px,例如厚度:2 px它太棒了,所有的两边都相等,但是2px对我来说太大了.

下面是我编辑模板后的代码:

代码语言:javascript
运行
复制
<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>

就像你们看到的

代码语言:javascript
运行
复制
<Setter Property="BorderBrush" Value="Purple"/>
                    <Setter Property="BorderThickness" Value="1"/> 

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-01-04 16:06:33

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

代码语言:javascript
运行
复制
<ComboBox Name="cmbComboBoxOne" ... SnapsToDevicePixels="True" UseLayoutRounding="True" />

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

编辑:将ClassicBorderDecorator替换为普通边框元素:

代码语言:javascript
运行
复制
<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>
票数 2
EN

Stack Overflow用户

发布于 2017-01-04 16:11:04

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41467883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档