PasswordBox和TextBox的通用样式

时间:2009-12-16 08:57:17

标签: wpf xaml

如何为TextBox和PasswordBox定义公共样式?

我的方法是,我已经为TargetType FrameworkElement定义了一个样式,但这不是常用样式,因此FrameworkElement上不存在很少的属性。

我的TextBox样式等于PasswordBoxStyle

<Style TargetType="{x:Type TextBox}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="MinHeight" Value="30"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="FontFamily" Value="Verdana"/>
    <Setter Property="FontSize" Value="14"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Border Name="Border" CornerRadius="4" Padding="2" Background="White" BorderBrush="Black" BorderThickness="1" >
                    <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:15)

尝试这样的事情

<Style x:Key="textboxPasswordboxStyles">
  <Setter Property="Control.ControlProperty" Value="value" />
  <Setter Property="TextBox.TextboxSpecproperty" Value="value" />
  <Setter Property="PasswordBox.PasswordBoxSpecProperty" Value="value" />
</Style>
<Style TargetType="{x:Type TextBox}"
    BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>
<Style TargetType="{x:Type PasswordBox}"
    BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>