Wpf 边框宽度显示不一致

Wpf 边框宽度显示不一致,wpf,xaml,textbox,styles,border,Wpf,Xaml,Textbox,Styles,Border,让我们以此为例。我现在一共有8个文本框。我使用静态资源样式来确保它们都具有完全相同的样式集。但是请注意,有些文本框有一条底线,而有些则没有。为什么会发生这种情况 这是密码 <Style x:Key="AddressTextBox" TargetType="TextBox"> <Setter Property="MinWidth" Value="230"></Setter> <Setter Prop

让我们以此为例。我现在一共有8个文本框。我使用静态资源样式来确保它们都具有完全相同的样式集。但是请注意,有些文本框有一条底线,而有些则没有。为什么会发生这种情况

这是密码

<Style x:Key="AddressTextBox" TargetType="TextBox">            
        <Setter Property="MinWidth" Value="230"></Setter>
        <Setter Property="MaxWidth" Value="260"></Setter>
        <Setter Property="MaxLength" Value="45"></Setter>
        <Setter Property="Margin" Value="1"></Setter>
        <Setter Property="BorderThickness" Value="1,1,1,1"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Padding" Value="1,2,0,1"/>
        <Setter Property="BorderBrush" Value="Gray"></Setter>
        <Setter Property="Height" Value="20"></Setter>
    </Style>

<DockPanel>
    <StackPanel>
        <Grid Margin="5">
            <StackPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7">Postal</TextBlock>
                        <TextBox Style="{StaticResource AddressTextBox}"></TextBox>                                            
                </DockPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7"></TextBlock>
                    <TextBox Style="{StaticResource AddressTextBox}"></TextBox>
                </DockPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7"></TextBlock>
                    <TextBox Style="{StaticResource AddressTextBox}"></TextBox>
                </DockPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7"></TextBlock>
                    <TextBox Style="{StaticResource AddressTextBox}"></TextBox>
                </DockPanel>
                <DockPanel Height="10"></DockPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7">Street</TextBlock>
                    <TextBox Style="{StaticResource AddressTextBox}"></TextBox>
                </DockPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7"></TextBlock>
                    <TextBox Style="{StaticResource AddressTextBox}"></TextBox>
                </DockPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7"></TextBlock>
                    <TextBox Style="{StaticResource AddressTextBox}"></TextBox>
                </DockPanel>
                <DockPanel Height="Auto">
                    <TextBlock Width="50" Margin="7"></TextBlock>
                    <TextBox Style="{StaticResource AddressTextBox}"></TextBox>
                </DockPanel>
            </StackPanel>
        </Grid>
    </StackPanel>
</DockPanel> 

邮政的
街头

尽管您的布局效率很低,但这并不是上述所有评论所暗示的问题。与
SnapToDevicePixels
填充
边距
等无关。它是
文本框
控件样式的一部分。看起来,如果您将
BorderWidth
设置为大于默认值,它会粘在所有角上,但如果设置为低于默认值,则不会粘在所有角上。如果提取
文本框的模板,可以看到其边框和样式。因此,为了“克服”这种不规则性,您需要覆盖其模板,而不是试图间接操纵样式中的
文本框
边框
属性。然后直接在
模板中操作它的
边框

下面是一个可以使用的样式(我将您的setters插入其中):


第一个PaymentTerms文本框中的BorderThickness为BorderThickness=“0,0,0,2”,而其他文本框中的BorderThickness为BorderThickness=“0,0,0,1”?您好,抱歉,这是一个输入错误-在更正后,问题仍然存在。您周围的面板是什么?我已经用StackPanel将您的代码复制/粘贴到一个空表单中,
,它看起来很好。
文本框
,它“看起来更厚”尝试使用
SnapsToDevicePixels=True
。复制/粘贴您的新标记,它在我的电脑上看起来也很好。我假设您周围有一些东西可以防止DockPanel耗尽所有空间,并且由于大小计算问题,一些像素被裁剪。您是否在空白的新WPF应用程序中尝试过示例代码?另外,您有哪些规格(.net/OS)-由于文本似乎模糊,因此肯定存在像素绘制问题。你试过@AnatoliyNikolaev推荐的SnapsToDevicePixels吗
<SolidColorBrush x:Key="DisabledBackgroundBrush" Color="LightGray"/>
    <SolidColorBrush x:Key="DisabledForegroundBrush" Color="Gray"/>
    <SolidColorBrush x:Key="EnabledBackgroundBrush" Color="White"/>

    <Style x:Key="AddressTextBox" TargetType="{x:Type TextBoxBase}">
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="OverridesDefaultStyle" Value="True"/>
        <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="MinWidth" Value="230"/>
        <Setter Property="MaxWidth" Value="260"/>
        <Setter Property="Margin" Value="1"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Padding" Value="1,2,0,1"/>
        <Setter Property="Height" Value="20"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Border Name="Border" CornerRadius="2"  Padding="2" Background="{StaticResource EnabledBackgroundBrush}" 
                            BorderBrush="Gray" BorderThickness="1" >
                        <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                            <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/>
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
<Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBlock Text="Postal"/>
        <TextBox Grid.Column="1" Style="{StaticResource AddressTextBox}"/>

        <TextBlock Grid.Row="1" />
        <TextBox Grid.Row="1" Grid.Column="1"  Style="{StaticResource AddressTextBox}"/>

        <TextBlock Grid.Row="2" />
        <TextBox Grid.Row="2" Grid.Column="1"  Style="{StaticResource AddressTextBox}"/>

        <TextBlock Grid.Row="3" />
        <TextBox Grid.Row="3" Grid.Column="1"  Style="{StaticResource AddressTextBox}"/>

        <TextBlock Grid.Row="4" Text="Street" Margin="7,10,7,7"/>
        <TextBox Grid.Row="4" Grid.Column="1"  Style="{StaticResource AddressTextBox}"/>

        <TextBlock Grid.Row="5"/>
        <TextBox Grid.Row="5" Grid.Column="1" Style="{StaticResource AddressTextBox}"/>

        <TextBlock Grid.Row="6"/>
        <TextBox Grid.Row="6" Grid.Column="1"  Style="{StaticResource AddressTextBox}"/>

        <TextBlock Grid.Row="7"/>
        <TextBox Grid.Row="7" Grid.Column="1"  Style="{StaticResource AddressTextBox}"/>
    </Grid>