3

让我们以此为例。我现在总共有 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> 
4

1 回答 1

1

尽管您的布局非常低效,但这并不是上述所有评论所暗示的问题。SnapToDevicePixels与, Padding,等无关Margins。它是TextBox's 控制风格的一部分。似乎如果你设置BorderWidth的比默认值大,它会粘在所有角落,但如果你低于它就不会。如果您提取TextBox的模板,您可以看到它的边框和样式。因此,为了“击败”这种不规则性,您需要覆盖其模板,而不是尝试间接操纵样式中的TextBox's属性。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>
于 2013-07-11T15:40:45.167 回答