如何在 Windows Phone 中制作带下划线的输入文本字段? [英] How to make an underlined input text field in Windows Phone?

查看:26
本文介绍了如何在 Windows Phone 中制作带下划线的输入文本字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

制作看起来像带下划线的输入文本字段的 TextBox 的最简单和正确的方法是什么?例如,您可以在消息中心和诺基亚传输的屏幕顶部看到我的意思的示例.

What is the easiest and correct way to make a TextBox, which is looks like an underlined input text field? You can see examples of what I mean, at the top of the screen in the Messaging Hub and in the Nokia Transport for example.

我应该使用什么技术来实现它?

What technique should I use for implementing that?

推荐答案

也许这是一个简单的方法:

May be this is an easy way:

  • 首先,将文本框和边框的默认背景设置为透明.

  • First, set default background of textbox and border to transparent.

然后您可以使用 Shawn Kendrot 的答案来获得下划线(因为您需要在文本框中添加标签)

Then you could use Shawn Kendrot's answer to get an underline (because of you need a label inside the textbox)

最后,要使焦点文本框背景透明,您必须遵循此 在 WP7 中更改焦点文本框背景/前景

And final, to make the focused textbox background transparent you must follow this Change focused textbox background/foreground in WP7

或者您可以创建一个样式,将自定义文本框模板设置为透明背景并插入下划线.

Or you could create a style that custom textbox template to transparent background and insert underline.

在我看来,最好的方法是尝试创建一个具有 Label 属性的 UserControl,您可以根据需要动态设置文本框标签.

In my opinion, the best way is try to create a UserControl have Label property, you could set textbox label dynamically while you need.

我只是创建了这个:

namespace Test
{
    public partial class CustomTextbox : UserControl
    {
        public readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(CustomTextbox), null);
        public readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(CustomTextbox), null);

        /// <summary>
        /// Get/set label
        /// </summary>
        public string Label
        {
            get
            {
                return (string)GetValue(LabelProperty);
            }
            set
            {
                SetValue(LabelProperty, value);
            }
        }

        /// <summary>
        /// Get/set text property
        /// </summary>
        public string Text
        {
            get
            {
                return (string)GetValue(TextProperty);
            }
            set
            {
                SetValue(TextProperty, value);
            }
        }

        public CustomTextbox()
        {
            InitializeComponent();

            DataContext = this;
        }
    }
}

XAML:

    <Border BorderBrush="White" BorderThickness="0,0,0,1">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Path=Label}" VerticalAlignment="Bottom" Margin="0,0,0,2" />
            <TextBox Text="{Binding Path=Text}" FontSize="22" Grid.Column="1" Background="Transparent" BorderThickness="1" Foreground="White" Margin="0" Padding="10,0" VerticalAlignment="Bottom">
                <TextBox.Template>
                    <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.TargetName="EnabledBorder" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Collapsed</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="ReadOnly">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="EnabledBorder" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Collapsed</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="Visibility">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <Visibility>Visible</Visibility>
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyBorder" Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="DisabledOrReadonlyContent" Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneTextBoxReadOnlyBrush}" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="FocusStates">
                                    <VisualState x:Name="Focused">
                                        <Storyboard>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unfocused" />
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>

                            <ContentControl x:Name="ContentElement"
                                                Margin="{StaticResource PhoneTextBoxInnerMargin}"
                                                HorizontalContentAlignment="Stretch"
                                                VerticalContentAlignment="Stretch"
                                                BorderThickness="0"
                                                Padding="{TemplateBinding Padding}" />

                            <Border x:Name="DisabledOrReadonlyBorder"
                                    Margin="{StaticResource PhoneTouchTargetOverhang}"
                                    Background="Transparent"
                                    BorderBrush="{StaticResource PhoneDisabledBrush}"
                                    BorderThickness="{TemplateBinding BorderThickness}"
                                    Visibility="Collapsed">
                                <TextBox x:Name="DisabledOrReadonlyContent"
                                         Background="Transparent"
                                         FontFamily="{TemplateBinding FontFamily}"
                                         FontSize="{TemplateBinding FontSize}"
                                         FontStyle="{TemplateBinding FontStyle}"
                                         FontWeight="{TemplateBinding FontWeight}"
                                         Foreground="{StaticResource PhoneDisabledBrush}"
                                         IsReadOnly="True"
                                         SelectionBackground="{TemplateBinding SelectionBackground}"
                                         SelectionForeground="{TemplateBinding SelectionForeground}"
                                         Text="{TemplateBinding Text}"
                                         TextAlignment="{TemplateBinding TextAlignment}"
                                         TextWrapping="{TemplateBinding TextWrapping}" 
                                 BorderThickness="0"/>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </TextBox.Template>
            </TextBox>
        </Grid>
    </Border>
</UserControl>

使用:

<local:CustomTextbox Label="label:" Text="text"></local:CustomTextbox>

这篇关于如何在 Windows Phone 中制作带下划线的输入文本字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆