边框宽度显示不一致

时间:2013-06-26 07:15:35

标签: wpf xaml textbox styles border

让我们以此为例。我现在总共有8个文本框。我使用静态资源样式来确保它们都具有完全相同的样式集。但请注意一些文本框如何具有底部边框线而其他文本框没有。为什么会这样?

enter image description here 这是代码

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

1 个答案:

答案 0 :(得分:1)

即使您的布局效率非常低,但并不像上述所有评论所暗示的那样存在问题。与SnapToDevicePixelsPaddingMargins等无关。它是TextBox'控件样式的一部分。似乎如果你设置BorderWidth大于默认值,它会粘在所有角落,但如果你低于它就不会。如果您提取TextBox的模板,则可以看到其边框和样式。因此,为了“击败”这种不规则性,您需要覆盖其模板,而不是试图间接操纵您的样式中的TextBox Border属性。然后直接在Border

中操纵它Template

这是一种可行的风格(我将你的二传手插入其中):

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