【问题标题】:Common Style for PasswordBox and TextBoxPasswordBox 和 TextBox 的通用样式
【发布时间】:2009-12-16 08:57:17
【问题描述】:

如何为 TextBox 和 PasswordBox 定义通用样式?

我的方法,我为 TargetType FrameworkElement 定义了一个样式,但这不能作为通用样式,然后 FrameworkElement 上不存在一些属性。

我的文本框样式等于密码框样式

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

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    试试这样的

    <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.MinHeight thx :)
    • @MarioBinder 您是如何解决有关属性的错误的?我也有同样的问题。我不应该将 style 属性放在 App.xaml 中吗?在资源目录中.....
    【解决方案2】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-16
      • 1970-01-01
      • 2013-12-18
      • 2011-05-04
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      相关资源
      最近更新 更多