实现带题头的TextBox和PasswordBox

WP开发 专栏收录该内容
4 篇文章 0 订阅

今天在网上看到一篇修改TextBox和PasswordBox模板的文章,很值得学习学习:

在Blend中创建一个新的TextBox,然后右键-Edit Template-Edit a Copy。在弹出的对话框中的Name设定一个名字,如“LabelTextBox”,然后在Define in中选择Application以在整个程序范围内都可以使用它。


操作完这些以后Blend会自动在App.xaml里创建一个新的Style资源,我们在里面搜索一下ContentControl,这就是TextBox中显示文字的元素了。剩下的就是对这个元素进行改造,如调整位置,添加新元素等等。下面是我写的TextBox和PasswordBox的资源:
    <controltemplate x:key="PhoneDisabledTextBoxTemplate" targettype="TextBox">  
        <contentcontrol x:name="ContentElement" borderthickness="0" horizontalcontentalignment="Stretch" margin="{StaticResource PhoneTextBoxInnerMargin}" padding="{TemplateBinding Padding}" verticalcontentalignment="Stretch">  
    </contentcontrol></controltemplate>  
    <style x:key="LabelTextBox" targettype="TextBox">  
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>  
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>  
        <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>  
        <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>  
        <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>  
        <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>  
        <Setter Property="SelectionForeground" Value="{StaticResource PhoneTextBoxSelectionForegroundBrush}"/>  
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>  
        <Setter Property="Padding" Value="2"/>  
        <Setter Property="Template">  
            <Setter.Value>  
                <ControlTemplate TargetType="TextBox">  
                    <Grid Background="Transparent">  
                        <VisualStateManager.VisualStateGroups>  
                            <VisualStateGroup x:Name="CommonStates">  
                                <VisualState x:Name="Normal"/>  
                                <VisualState x:Name="MouseOver"/>  
                                <VisualState x:Name="Disabled">  
                                    <Storyboard>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0">  
                                                <DiscreteObjectKeyFrame.Value>  
                                                    <Visibility>Collapsed</Visibility>  
                                                </DiscreteObjectKeyFrame.Value>  
                                            </DiscreteObjectKeyFrame>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0">  
                                                <DiscreteObjectKeyFrame.Value>  
                                                    <Visibility>Visible</Visibility>  
                                                </DiscreteObjectKeyFrame.Value>  
                                            </DiscreteObjectKeyFrame>  
                                        </ObjectAnimationUsingKeyFrames>  
                                    </Storyboard>  
                                </VisualState>  
                                <VisualState x:Name="ReadOnly">  
                                    <Storyboard>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0">  
                                                <DiscreteObjectKeyFrame.Value>  
                                                    <Visibility>Collapsed</Visibility>  
                                                </DiscreteObjectKeyFrame.Value>  
                                            </DiscreteObjectKeyFrame>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0">  
                                                <DiscreteObjectKeyFrame.Value>  
                                                    <Visibility>Visible</Visibility>  
                                                </DiscreteObjectKeyFrame.Value>  
                                            </DiscreteObjectKeyFrame>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="DisabledOrReadonlyBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}"/>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="DisabledOrReadonlyContent">  
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}"/>  
                                        </ObjectAnimationUsingKeyFrames>  
                                    </Storyboard>  
                                </VisualState>  
                            </VisualStateGroup>  
                            <VisualStateGroup x:Name="FocusStates">  
                                <VisualState x:Name="Focused">  
                                    <Storyboard>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EnabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="EnabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}"/>  
                                        </ObjectAnimationUsingKeyFrames>  
                                    </Storyboard>  
                                </VisualState>  
                                <VisualState x:Name="Unfocused"/>  
                            </VisualStateGroup>  
                        </VisualStateManager.VisualStateGroups>  
                        <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">  
                            <Grid>  
                                <Grid.ColumnDefinitions>  
                                    <ColumnDefinition Width="Auto" />  
                                    <ColumnDefinition Width="*" />  
                                </Grid.ColumnDefinitions>  
                                <TextBlock Grid.Column="0" Margin="10, 0" Text="{TemplateBinding Tag}" VerticalAlignment="Center" />  
                                <ContentControl Grid.Column="1" x:Name="ContentElement" BorderThickness="0" HorizontalContentAlignment="Stretch" Margin="1,2,1,2" Padding="{TemplateBinding Padding}" VerticalContentAlignment="Stretch" VerticalAlignment="Center"/>  
                            </Grid>  
                        </Border>  
                        <Border x:Name="DisabledOrReadonlyBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed">  
                            <Grid>  
                                <Grid.ColumnDefinitions>  
                                    <ColumnDefinition Width="Auto" />  
                                    <ColumnDefinition Width="*" />  
                                </Grid.ColumnDefinitions>  
                                <TextBlock Grid.Column="0" Margin="10, 0" Text="{TemplateBinding Tag}" VerticalAlignment="Center" Foreground="{StaticResource PhoneDisabledBrush}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" />  
                                <TextBox Grid.Column="1" x:Name="DisabledOrReadonlyContent" Background="Transparent" Foreground="{StaticResource PhoneDisabledBrush}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" IsReadOnly="True" SelectionForeground="{TemplateBinding SelectionForeground}" SelectionBackground="{TemplateBinding SelectionBackground}" TextAlignment="{TemplateBinding TextAlignment}" TextWrapping="{TemplateBinding TextWrapping}" Text="{TemplateBinding Text}" Template="{StaticResource PhoneDisabledTextBoxTemplate}"/>  
                            </Grid>  
                        </Border>  
                    </Grid>  
                </ControlTemplate>  
            </Setter.Value>  
        </Setter>  
    </style>  
    <controltemplate x:key="PhoneDisabledPasswordBoxTemplate" targettype="PasswordBox">  
        <border x:name="ContentElement" borderthickness="0" margin="{StaticResource PhonePasswordBoxInnerMargin}" padding="{TemplateBinding Padding}">  
    </border></controltemplate>  
    <style x:key="LabelPassword" targettype="PasswordBox">  
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/>  
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMediumLarge}"/>  
        <Setter Property="Background" Value="{StaticResource PhoneTextBoxBrush}"/>  
        <Setter Property="Foreground" Value="{StaticResource PhoneTextBoxForegroundBrush}"/>  
        <Setter Property="BorderBrush" Value="{StaticResource PhoneTextBoxBrush}"/>  
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>  
        <Setter Property="SelectionBackground" Value="{StaticResource PhoneAccentBrush}"/>  
        <Setter Property="SelectionForeground" Value="{StaticResource PhoneContrastBackgroundBrush}"/>  
        <Setter Property="Padding" Value="2"/>  
        <Setter Property="Template">  
            <Setter.Value>  
                <ControlTemplate TargetType="PasswordBox">  
                    <Grid Background="Transparent">  
                        <VisualStateManager.VisualStateGroups>  
                            <VisualStateGroup x:Name="CommonStates">  
                                <VisualState x:Name="Normal"/>  
                                <VisualState x:Name="MouseOver"/>  
                                <VisualState x:Name="Disabled">  
                                    <Storyboard>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="EnabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0">  
                                                <DiscreteObjectKeyFrame.Value>  
                                                    <Visibility>Collapsed</Visibility>  
                                                </DiscreteObjectKeyFrame.Value>  
                                            </DiscreteObjectKeyFrame>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="DisabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0">  
                                                <DiscreteObjectKeyFrame.Value>  
                                                    <Visibility>Visible</Visibility>  
                                                </DiscreteObjectKeyFrame.Value>  
                                            </DiscreteObjectKeyFrame>  
                                        </ObjectAnimationUsingKeyFrames>  
                                    </Storyboard>  
                                </VisualState>  
                            </VisualStateGroup>  
                            <VisualStateGroup x:Name="FocusStates">  
                                <VisualState x:Name="Focused">  
                                    <Storyboard>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="EnabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBackgroundBrush}"/>  
                                        </ObjectAnimationUsingKeyFrames>  
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="EnabledBorder">  
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxEditBorderBrush}"/>  
                                        </ObjectAnimationUsingKeyFrames>  
                                    </Storyboard>  
                                </VisualState>  
                                <VisualState x:Name="Unfocused"/>  
                            </VisualStateGroup>  
                        </VisualStateManager.VisualStateGroups>  
                        <Border x:Name="EnabledBorder" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}">  
                            <Grid>  
                                <Grid.ColumnDefinitions>  
                                    <ColumnDefinition Width="Auto" />  
                                    <ColumnDefinition Width="*" />  
                                </Grid.ColumnDefinitions>  
                                <TextBlock Grid.Column="0" Margin="10, 0" Text="{TemplateBinding Tag}" VerticalAlignment="Center" />  
                                <Border Grid.Column="1" x:Name="ContentElement" BorderThickness="0" Margin="{StaticResource PhonePasswordBoxInnerMargin}" Padding="{TemplateBinding Padding}"/>  
                            </Grid>  
                        </Border>  
                        <Border x:Name="DisabledBorder" BorderBrush="{StaticResource PhoneDisabledBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="Transparent" Margin="{StaticResource PhoneTouchTargetOverhang}" Visibility="Collapsed">  
                            <Grid>  
                                <Grid.ColumnDefinitions>  
                                    <ColumnDefinition Width="Auto" />  
                                    <ColumnDefinition Width="*" />  
                                </Grid.ColumnDefinitions>  
                                <TextBlock Grid.Column="0" Margin="10, 0" Text="{TemplateBinding Tag}" VerticalAlignment="Center" Foreground="{StaticResource PhoneDisabledBrush}" FontWeight="{TemplateBinding FontWeight}" FontStyle="{TemplateBinding FontStyle}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" />  
                                <PasswordBox Grid.Column="1" x:Name="DisabledContent" Background="Transparent" Foreground="{StaticResource PhoneDisabledBrush}" Password="{TemplateBinding Password}" PasswordChar="{TemplateBinding PasswordChar}" Template="{StaticResource PhoneDisabledPasswordBoxTemplate}"/>  
                            </Grid>  
                        </Border>  
                    </Grid>  
                </ControlTemplate>  
            </Setter.Value>  
        </Setter>  
    </style>  
Ok,样式设定完毕,要使用的时候就非常简单了,在项目里的任何一个TextBox或者PasswordBox中像这样使用它就可以了:
    <textbox tag="帐号:" style="{StaticResource LabelTextBox}" name="Username">  
    <passwordbox tag="密码:" style="{StaticResource LabelPassword}" name="Password">  
    </passwordbox></textbox>  

大功告成!其中Tag是控件前面题头里的文字,而TextBox和PasswordBox的功能不会受到任何影响~

原文地址:http://blog.zkd.me/archives/33

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值