首页 > 代码库 > WPF中的TextBox一些问题
WPF中的TextBox一些问题
1、如何判断TextBox是否为空?
if(textbox.Text != string.empty)//或者if(textbox.Text.Trim()!=""){}
2、设置TextBox为空时,背景文字提示
参考博客:http://www.cnblogs.com/tsunami/archive/2011/09/16/2179170.html
<TextBox FontSize="17" Height="26" Margin="230,150,189,0" Name="txt_Account" VerticalAlignment="Top" Foreground="Indigo" TabIndex="0" BorderThickness="1"> <TextBox.Resources> <VisualBrush x:Key="HelpBrush" TileMode="None" Opacity="0.3" Stretch="None" AlignmentX="Left"> <VisualBrush.Visual> <TextBlock FontStyle="Italic" Text="请输入用户名"/> </VisualBrush.Visual> </VisualBrush> </TextBox.Resources> <TextBox.Style> <Style TargetType="TextBox"> <Style.Triggers> <Trigger Property="Text" Value="{x:Null}"> <Setter Property="Background" Value="{StaticResource HelpBrush}"/> </Trigger> <Trigger Property="Text" Value=""> <Setter Property="Background" Value="{StaticResource HelpBrush}"/> </Trigger> </Style.Triggers> </Style> </TextBox.Style> </TextBox>
在Stackoverflow上的回答也很好http://stackoverflow.com/questions/833943/watermark-hint-text-placeholder-textbox-in-wpf/21672408#21672408
以下这个回答使用资源实现
<Window.Resources> <Style x:Key="TextBoxUserStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}"> <Setter Property="Foreground" Value="Black"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Width" Value="225"/> <Setter Property="Height" Value="25"/> <Setter Property="FontSize" Value="12"/> <Setter Property="Padding" Value="1"/> <Setter Property="Margin" Value="5"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type TextBox}"> <Border x:Name="OuterBorder" BorderBrush="#5AFFFFFF" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4"> <Border x:Name="InnerBorder" Background="#FFFFFFFF" BorderBrush="#33000000" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3"> <ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="PART_ContentHost"/> </Border> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="PasswordBoxVistaStyle" BasedOn="{x:Null}" TargetType="{x:Type PasswordBox}"> <Setter Property="Foreground" Value="Black"/> <Setter Property="HorizontalAlignment" Value="Center"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="Width" Value="225"/> <Setter Property="Height" Value="25"/> <Setter Property="FontSize" Value="12"/> <Setter Property="Padding" Value="1"/> <Setter Property="Margin" Value="5"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type PasswordBox}"> <Border x:Name="OuterBorder" BorderBrush="#5AFFFFFF" BorderThickness="1,1,1,1" CornerRadius="4,4,4,4"> <Border x:Name="InnerBorder" Background="#FFFFFFFF" BorderBrush="#33000000" BorderThickness="1,1,1,1" CornerRadius="3,3,3,3"> <Grid> <Label x:Name="lblPwd" Content="Password" FontSize="11" VerticalAlignment="Center" Margin="2,0,0,0" FontFamily="Verdana" Foreground="#828385" Padding="0"/> <ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="PART_ContentHost"/> </Grid> </Border> </Border> <ControlTemplate.Triggers> <Trigger Property="IsFocused" Value="True"> <Setter Property="Visibility" TargetName="lblPwd" Value="Hidden"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <PasswordBox Style="{StaticResource PasswordBoxVistaStyle}" Margin="169,143,22,0" Name="txtPassword" FontSize="14" TabIndex="2" Height="31" VerticalAlignment="Top" />
使用第一种方式实现PasswordBox的话需要设置依赖属性,因为PasswordBox中的Password不是依赖属性,需要自己实现。留作以后研究
3、自己写UserControl实现PasswordBox和TextBox的水印
参考博客:http://www.cnblogs.com/zhanglee/archive/2012/10/20/2732097.html
贴出PasswordBox的代码,侵删
<UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication1.MyPassword" x:Name="UserControl" d:DesignWidth="150" d:DesignHeight="26"> <Grid x:Name="LayoutRoot"> <Border Background="White" Padding="2,3" Margin="1" d:LayoutOverrides="VerticalAlignment, GridBox"/> <TextBlock Text="请输入密码" x:Name="txtPasswordTip" Padding="7,3" Margin="1,3,1,1" FontFamily="SimSun" d:LayoutOverrides="VerticalAlignment, GridBox"/> <PasswordBox Padding="4,3,2,3" Background="Transparent" x:Name="password" PasswordChanged="password_PasswordChanged" d:LayoutOverrides="GridBox" Password=""/> </Grid></UserControl>
using System;using System.Collections.Generic;using System.Text;using System.Windows;using System.Windows.Controls;using System.Windows.Data;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Imaging;using System.Windows.Navigation;using System.Windows.Shapes;namespace WpfApplication1{ /// <summary> /// MyPassword.xaml 的交互逻辑 /// </summary> public partial class MyPassword : UserControl { public MyPassword() { this.InitializeComponent(); if (string.IsNullOrWhiteSpace(TipContent)) { _tipContent = "请输入密码"; } txtPasswordTip.Text = TipContent; } string tip = string.Empty; private void password_PasswordChanged(object sender, RoutedEventArgs e) { if (password.Password.Length != 0) { // this.Content = password.Password; this._Password = password.Password; if (!string.IsNullOrWhiteSpace(txtPasswordTip.Text)) tip = txtPasswordTip.Text; txtPasswordTip.Text = string.Empty; } else { txtPasswordTip.Text = tip; } } private string _tipContent = string.Empty; public string TipContent { get { return _tipContent; } set { if (value != _tipContent) { _tipContent = value; txtPasswordTip.Text = TipContent; } } } private string _Password = string.Empty; public string Password { get { return _Password; } set { if (value != _Password) { _Password = value; } } } }}
待更新.......
WPF中的TextBox一些问题
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。