TextBox und PasswordBox mit gemeinsamen Style
- Dezember 16th, 2009
- Posted in Development . MarkUp . WPF . XAML
- Kommentar schreiben
Folgendes HowTo zeigt, wie man für die PasswordBox und der TextBox einen gemeinsamen Style im XAML definiert.
Zuerst definiert man die grundlegenden Einstellungen
1 2 3 4 5 6 7 8 | <System:Double x:Key="MinHeight">30</System:Double> <System:Double x:Key="FontSize">14</System:Double> <FontFamily x:Key="FontFamily">Verdana</FontFamily> <ControlTemplate x:Key="ControlTemplate"> <Border Name="Border" CornerRadius="4" Padding="2" Background="White" BorderBrush="Black" BorderThickness="1" > <ScrollViewer Margin="0" x:Name="PART_ContentHost"/> </Border> </ControlTemplate> |
dann kann man über die Punktnotation über ein Control direkt auf das Property zugreifen:
1 | <Setter Property="TextBox.MinHeight" Value="30" /> |
Und so definieren wir auch die Gemeinsamkeiten und vergeben hier einen Key (CommonStyle)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <Style x:Key="CommonStyle"> <Setter Property="TextBox.MinHeight" Value="{StaticResource MinHeight}" /> <Setter Property="PasswordBox.MinHeight" Value="{StaticResource MinHeight}" /> <Setter Property="TextBox.SnapsToDevicePixels" Value="True"/> <Setter Property="PasswordBox.SnapsToDevicePixels" Value="True"/> <Setter Property="TextBox.OverridesDefaultStyle" Value="True"/> <Setter Property="PasswordBox.OverridesDefaultStyle" Value="True"/> <Setter Property="TextBox.FocusVisualStyle" Value="{x:Null}"/> <Setter Property="PasswordBox.FocusVisualStyle" Value="{x:Null}"/> <Setter Property="TextBox.AllowDrop" Value="true"/> <Setter Property="PasswordBox.AllowDrop" Value="true"/> <Setter Property="TextBox.FontFamily" Value="{StaticResource FontFamily}"/> <Setter Property="PasswordBox.FontFamily" Value="{StaticResource FontFamily}"/> <Setter Property="TextBox.FontSize" Value="{StaticResource FontSize}"/> <Setter Property="PasswordBox.FontSize" Value="{StaticResource FontSize}"/> <Setter Property="TextBox.Template" Value="{StaticResource ControlTemplate}"/> <Setter Property="PasswordBox.Template" Value="{StaticResource ControlTemplate}"/> </Style> |
und nun legen wir den Style auf die beiden Controls
CODE
1 2 3 4 5 | <!-- TextBoxStyle --> <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource CommonStyle}" /> <!-- PasswordBoxStyle --> <Style TargetType="{x:Type PasswordBox}" BasedOn="{StaticResource CommonStyle}" /> |
Viel Spass beim entwickeln : )
Ähnliche Beiträge
Auch wenn Du nichts zu diesem Thema sagen möchtest, bringst du mit einem Klick auf den Like- oder KickButton zum Ausdruck, dass dir dieser Artikel gefällt. Vielen Dank : )




Noch keine Kommentare.