密码框和文本框的常见样式

8

我该如何为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>
2个回答

16

试着像这样做

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

样式属性“PasswordBoxSpecProperty”在“System.Windows.Controls.PasswordBox”中找不到。对于“ControlProperty”、“System.Windows.Controls.Control”和“TextboxSpecproperty”、“System.Windows.Controls.TextBox”也是如此。 - Mario Binder
啊哈,好的我明白了...我解决了...TextBox.MinHeight谢谢:) - Mario Binder
@MarioBinder,你是怎么解决关于属性的错误的?我也遇到了同样的问题。难道我不应该把样式属性放在App.xaml中吗?或者放在资源目录中…… - Tarek

3
<Style x:Key="textboxPasswordboxStyles" TargetType="Control">
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None" />
    <Setter Property="Height" Value="20" />
    <Setter Property="MinHeight" Value="20" />
    <Setter Property="MaxHeight" Value="22"/>
    <Setter Property="BorderThickness" Value="1.5"/>
    <Setter Property="AllowDrop" Value="true" />
    <Setter Property="Template">
        <Setter.Value>

            <ControlTemplate TargetType="{x:Type Control}">
                <Grid>
                    <Border Name="Border"  CornerRadius="8" BorderThickness="{TemplateBinding BorderThickness}" >
                        <Border.Background>
                            <SolidColorBrush Color="White" x:Name="Background" />
                        </Border.Background>

                        <Border.BorderBrush>
                            <SolidColorBrush Color="Gray" x:Name="BorderBrush" Opacity="1"/>
                        </Border.BorderBrush>

                        <ScrollViewer Margin="10,0,10,0" x:Name="PART_ContentHost" VerticalAlignment="Center"/>

                    </Border>





                </Grid>
            </ControlTemplate>
        </Setter.Value>

    </Setter>


</Style>

<Style TargetType="TextBox" 
BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>
<Style TargetType="PasswordBox" 
BasedOn="{StaticResource textboxPasswordboxStyles}">
</Style>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接