2013-04-02 54 views

回答

31

在官方Windows Phone Toolkit中有PhoneTextBox,涵蓋了here

示例代碼:

<toolkit:PhoneTextBox Hint="Password"/> 

enter image description here

要添加工具到您的項目: 鍵入以下到您的包管理器控制檯:
PM> Install-Package WPtoolkit

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

內012在xaml頁標籤

+1

感謝其幫助。 – John

+0

VerticalContentAlignment =「Top」不起作用?任何人都知道如何使它垂直對齊頂部(文本是) – Jimmyt1988

+0

有沒有辦法保持提示文本時,texbox接收焦點? –

1

沒有爲文本框沒有佔位符屬性。我用以下解決方案來解決此用戶名文本框:

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和一個TextBlock(作爲佔位符)。然後當文本框被集中時,文本塊被隱藏,當它失去焦點時,如果文本框爲空,則顯示文本塊。

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"/> 

可能不是最有效的方法,但它的工作原理。

0

嘗試這樣下面的代碼:

<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> 
相關問題