9

windows phone 8 中的 xaml 中的文本框是否有任何“占位符类型”属性可用

4

4 回答 4

31

在官方的Windows Phone 工具包中有 PhoneTextBox ,这里有介绍。

示例代码:

<toolkit:PhoneTextBox Hint="Password"/>

在此处输入图像描述

将工具包添加到您的项目:在您的包管理器控制台中输入以下内容:
PM> Install-Package WPtoolkit
并且

xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"

页面内<phone:PhoneApplicationPage标签xaml

于 2013-04-02T09:11:54.217 回答
1

TextBox 没有占位符属性。我使用以下解决方案来解决用户名文本框的问题:

XAML:

<Grid>
    <TextBlock Name="UsernamePlaceholder" Text="Username" />
    <TextBox Name="UsernameTextBox" Text="" GotFocus="TextBox_GotFocus" LostFocus="TextBox_LostFocus"/>
</Grid>

代码:

private void TextBox_GotFocus(object sender, RoutedEventArgs e) 
{    
    UsernamePlaceholder.Visibility = Visibility.Collapsed;
}

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    if (sender is TextBox)
    {
        var textbox = sender as TextBox;
        if (string.IsNullOrEmpty(textbox.Text))
        {
            UsernamePlaceholder.Visibility = Visibility.Visible;
        }
    }
}

这基本上将 TextBox 替换为 Grid 元素,其中包含一个 TextBox 和一个 TextBlock(用作占位符)。然后当文本框被聚焦时,文本块被隐藏,当它失去焦点时,如果文本框为空,则显示文本块。

于 2013-04-02T09:09:18.433 回答
1

试试下面的代码:

<TextBox x:Name="InvoiceDate" Text="" Width="300"  TextAlignment="Left" Height="30" Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="2" />
                    <TextBlock IsHitTestVisible="False" Text="Men att läsa" Width="300"  TextAlignment="Left" Height="30" Grid.Row="0" Grid.Column="3" Grid.ColumnSpan="2" Padding="5, 5, 5, 5"  Foreground="LightGray">
                        <TextBlock.Style>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Visibility" Value="Collapsed"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Text, ElementName=InvoiceDate}" Value="">
                                        <Setter Property="Visibility" Value="Visible"/>
                                    </DataTrigger>                                    
                                </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
于 2014-11-18T15:11:09.627 回答
0

我的解决方案是基于 PKENO 的回答

XAML(用户控件):

<UserControl x:Class="FestivalProject.Controls.TextBoxPH"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"            
             mc:Ignorable="d">
    <TextBox x:Name="testTextBox" Margin="0" LostFocus="testTextBox_LostFocus" GotFocus="testTextBox_GotFocus"/>
</UserControl>

用户控件背后的代码:

public partial class TextBoxPH : UserControl
    {
        private String _Text;

        public String Text
        {
            get { return _Text; }
            set { 
                _Text = testTextBox.Text = value;               
            }
        }



        private String _PlaceHolder;
        public String PlaceHolder
        {
            get { return _PlaceHolder; }
            set { 
                _PlaceHolder =testTextBox.Text = value;      
            }
        }        

        public TextBoxPH()
        {           
            InitializeComponent();  
        }   

        private void testTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(testTextBox.Text)) testTextBox.Text = PlaceHolder;              

        }

        private void testTextBox_GotFocus(object sender, RoutedEventArgs e)
        {
            if (testTextBox.Text.Equals(PlaceHolder, StringComparison.OrdinalIgnoreCase)) testTextBox.Text = string.Empty;

        }
    }

XAML(在窗口中):

<txtPH:TextBoxPH  Margin="5"  Grid.ColumnSpan="2" PlaceHolder="PlaceholderText"/>

可能不是最有效的方法,但它确实有效。

于 2013-10-23T22:19:31.357 回答