Sunday, May 3, 2015

Custom TextBox and PasswordBox in WPF



 <ControlTemplate x:Key="TextBoxBaseControlTemplate" TargetType="{x:Type TextBoxBase}">
            <Border Background="{TemplateBinding Background}"
                x:Name="Bd" BorderBrush="#FF95C0DB"
                BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10,10,10,10">
                <ScrollViewer x:Name="PART_ContentHost"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
                            TargetName="Bd"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
                <Trigger Property="Width" Value="Auto">
                    <Setter Property="MinWidth" Value="100"/>
                </Trigger>
                <Trigger Property="Height" Value="Auto">
                    <Setter Property="MinHeight" Value="40"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <ControlTemplate x:Key="PasswordBoxControlTemplate" TargetType="{x:Type PasswordBox}">
            <Border Background="{TemplateBinding Background}"
                x:Name="Bd" BorderBrush="#FF95C0DB"
                BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10,10,10,10">
                <ScrollViewer x:Name="PART_ContentHost"/>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" TargetName="Bd"/>
                    <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                </Trigger>
                <Trigger Property="Width" Value="Auto">
                    <Setter Property="MinWidth" Value="100"/>
                </Trigger>
                <Trigger Property="Height" Value="Auto">
                    <Setter Property="MinHeight" Value="40"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <Style x:Key="{x:Type PasswordBox}"
        TargetType="{x:Type PasswordBox}">
            <Setter Property="local:PasswordBoxMonitor.IsMonitoring"
              Value="True"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type PasswordBox}">
                        <Border Name="Bd" BorderBrush="#FF95C0DB"
                BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="10,10,10,10"
                    Background="#FF95C0DB"                  
                    SnapsToDevicePixels="true">
                            <Grid>
                                <ScrollViewer x:Name="PART_ContentHost"
                              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                                <TextBlock Text="Password"                         
                           Foreground="White" FontSize="12"
                  FontStyle="Italic" Margin="5,10,0,0"
                  FontFamily="HelveticaNeue-Light,HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica, Arial,Lucida Grande,sans-serif"
                           Visibility="Collapsed"
                           Name="txtPrompt" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled"
                                         Value="false">
                                <Setter TargetName="Bd"
                                            Property="Background"
                                            Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
                                <Setter Property="Foreground"
                                            Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                            <Trigger Property="local:PasswordBoxMonitor.PasswordLength" Value="0">
                                <Setter Property="Visibility" TargetName="txtPrompt" Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

 <TextBox Width="369" Margin="80,0,0,0" Height="32" Grid.Column="0" Grid.Row="3" MaxLength="500"  
                 TabIndex="1" HorizontalAlignment="Center" KeyDown="txtUser_KeyDown_1"
                 Name="txtUser" VerticalAlignment="Top"
                BorderBrush="{x:Null}" Foreground="White" HorizontalContentAlignment="Left"
                 VerticalContentAlignment="Center"
                 FontSize="12" FontStyle="Italic"
                 FontFamily="HelveticaNeue-Light,HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica, Arial,Lucida Grande,sans-serif"
                 Background="Transparent" Text=""
                 Template="{StaticResource TextBoxBaseControlTemplate}">
                </TextBox>
                <PasswordBox Margin="80,0,0,0" Width="369" Height="32" Grid.Column="0" Grid.Row="4" MaxLength="500"  
                 TabIndex="1" HorizontalAlignment="Center" KeyDown="txtPwd_KeyDown_1"
                 Name="txtPassword" VerticalAlignment="Center"
                 BorderBrush="{x:Null}" Foreground="White"
                 HorizontalContentAlignment="Left" VerticalContentAlignment="Center"
                 FontSize="12" FontStyle="Italic"
                 FontFamily="HelveticaNeue-Light,HelveticaNeue-Light,Helvetica Neue Light,Helvetica Neue,Helvetica, Arial,Lucida Grande,sans-serif"
                 Background="Transparent" ToolTip="Password">
                </PasswordBox>

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...