2009-12-16 5 views
5

Comment puis-je définir un style commun pour le TextBox et le PasswordBox?Style commun pour PasswordBox et TextBox

Mon approche, j'ai définir un style pour le TargetType FrameworkElement mais cela ne fonctionne pas comme style commun, alors peu de propertys n'existe pas sur le FrameworkElement.

Mon style TextBox est égale à la 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> 

Répondre

12

Essayez quelque chose comme ça

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

style doesnt propriété "PasswordBoxSpecProperty" trouvé dans "System.Windows.Controls.PasswordBox" même pour "ControlProperty" "System.Windows.Controls.Control" et "TextboxSpecproperty" "System.Windows.Controls.TextBox" –

+0

ah lol, d'accord, je comprends .. j'ai résolu ... TextBox.MinHeight thx:) –

+0

@MarioBinder comment avez-vous résolu l'erreur concernant les propriétés? J'ai le même problème. Ne devrais-je pas mettre la propriété de style dans le App.xaml? Dans un répertoire de ressources ... – Tarek

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