WPF--TextBox的验证】的更多相关文章

正则:^(0\.\d+|[1-9][0-9]|1)$ TextBox绑定正则验证 <TextBox x:Name="txb"   MaxLength="6" Margin="1 0 0 0"  Width="40" >    <TextBox.Text>        <Binding Path="Opacity" ValidatesOnExceptions="Tru…
原文:[WPF]数据验证 引言      数据验证在任何用户界面程序中都是不可缺少的一部分.在WPF中,数据验证更是和绑定紧紧联系在一起,下面简单介绍MVVM模式下常用的几种验证方式. 错误信息显示 在介绍数据验证之前,有必要介绍一下如何显示错误信息.方式很简单,定义一个样式触发器,将错误信息和 ToolTip绑定,如下: <Style TargetType="TextBox"> <Style.Triggers> <Trigger Property=&qu…
WPF TextBox 一些设置技巧 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-01 参考: 章节: 取消输入法 输入方式设定为Overwrite 限定输入字符数 一.取消输入法 TextBox txbx=new TextBox(); InputMethod.SetIsInputMethodEnabled(txbx, false);//关掉输入法 二.输入方式设定为Overwrite //把输入改成 overwrite 模式 //…
WPF TextBox框提示文字,鼠标划入提示文字消失 <TextBox Width=" VerticalContentAlignment="Center" BorderThickness=" CaretBrush="White"> <TextBox.Resources> <VisualBrush x:Key="HintText" AlignmentX="Left" Opaci…
//验证输入为数字private void txt_time_KeyDown(object sender, KeyEventArgs e){ if (!((e.Key >= Key.D0 && e.Key <= Key.D9) || (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9))) { e.Handled = true; }} //屏蔽粘贴非法字符private void txt_time_TextCh…
WPF 显示验证错误的默认方式是在控件周围绘制红色边框.通常需要对此方法进行自定义,以通过其他方式来显示错误.而且,默认情况下不会显示与验证错误关联的错误消息.常见的要求是仅当存在验证错误时才在工具提示中显示错误消息.通过将 Styles 和一组与验证关联的附加属性进行组合,可以相当轻松地自定义验证错误显示. 前台xaml: <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentatio…
Textbox加水印 Textbox加水印,需要一个VisualBrush和触发器验证Text是否为空,在空的时候设置背景的Brush就可以实现水印效果. <TextBox Name="txtBoxName" Width="120" Height="23"> <TextBox.Resources> <VisualBrush x:Key="HelpBrush" TileMode="None…
纠结了半天WPF下只读的TextBox怎么显示输入焦点提示,发现wpf 4中已有新属性“IsReadOnlyCaretVisible”,大善^_^…
今天调试自己写的WPF的Behavior, 是关于TextBox只能输入数据或者小数点的. 发现有个问题, 就是英文IME下字母等等都能过滤, 但是一旦切换到中文输入法, 就会发现在OnPreviewTextInput处理 时Textbox.Text已经得到中文输入的值,导致就算PreviewTextInput响应了,而且当前的中文也被过滤了,但是TextBox.Text已经有同样的中文存在, 表现出过滤失败. 原因可能是在中文输入后TextBox第一次得到值TextInput没有响应,而第二次…
在做表单的,需要对User提交数据做验证,wpf与silverlight 都提供自带的验证机制,但是只是验证,并不能在提交时提供详细的信息,此时可使用 依赖属性将错误信息整合进自定义错误集合中,即可在提交时获取相应错误信息方便后续业务处理. Silverlifgt版本: public class ValidationScope { public FrameworkElement ScopeElement { get; private set; } private readonly Observa…
更多资源:http://denghejun.github.io <Style x:Key="SearchTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}"> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlT…
test.zul: <?page title="" contentType="text/html;charset=UTF-8"?> <zk xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.zkoss.org/2005/zul" xsi:schemaLocation="http://www.zkoss.or…
textBox经常用来显示程序的运行状态或者消息,如何让他自动滚动呢? 在显示消息代码下加一条自动滚动到底部的语句即可: TextBox1.ScrollToEnd(); (如果要显示垂直滚动条设置VerticalScrollBarVisibility="Auto",如果不显示设置为Hidden) 我用的程序代码如下: this.Dispatcher.Invoke(new Action(() => { //大于100行清除记录,可选 if (txtReceived.LineCoun…
遇到一个需求,在textbox获得焦点的时候,调用一个外部的软键盘. 这可以用两个不同的方法来达到目的. 1.EventTrigger 首先定义一个Style <Style x:Key="TopSoftKeyboardTextBox" TargetType="{x:Type TextBox}"> <Setter Property="FontFamily" Value="黑体"/> <Setter…
在app.xaml中加入以下节点,全局设置textbox圆角 <Style TargetType="{x:Type TextBox}">            <Setter Property="VerticalAlignment" Value="Center"/>            <Setter Property="HorizontalAlignment" Value="Cent…
前两天做一个项目的时候,由于页面没有限制TextBox的输入长度,所以,后台直接报错了,超出数据库最大的长度. 数据库的长度是按照字节来计算的,而且不同的编码格式,汉字占用的字节长度又不相同,比如,我们用的是UTF8,一个汉字是3个字节,而默认的Default,一个汉字是2个字节. TextBox有个MaxLength属性,但是这个属性是不太合乎要求的,因为这个长度,是限制了输入的长度,比如设置20,则无论是数字.字母.汉字最大的长度都是20个,但是,对于数据库来说,长度却不相同了, 所以,不能…
文本框显示 文本框正常显示: 文本框超出区域显示: 实现方案 判断文本框是否超出区域 请见<TextBlock IsTextTrimmed 判断文本是否超出> 设置文本布局显示 1. FlowDirection 当文本超出显示区域时,设置FlowDirection靠右显示 下面是封装的附加属性ScrollEndWhenTextTrimmed /// <summary> /// 当文本超出显示时,文本是否靠右侧显示 /// </summary> public static…
TextBox纵向长度比较长但文字字体比较小的时候,在输入时就会发现文字不是垂直居中的. 而使用中我们发现,TextBox虽然可以设置文字的水平对齐方式,但却没有相应的属性让我们来调节他的垂直对齐方式.好在TextBox继承自Control类,可以通过修改Template模板来改变他的属性样式(TextBlock继承自FrameworkElement,不可进行模板编辑). <Style x:Key="Test_TextBox" TargetType="{x:Type T…
1.  Code behind. protected void TextBox1_TextChanged(object sender, EventArgs e) { Label1.Text = Server.HtmlEncode(TextBox1.Text); } 2.  Event trigger xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" <…
textbox.GotFocus 事件处理 Textbox.SelectAll() 是不行的, 这样处理会发生的情况是: 1) textbox1 当前没有焦点, 内容为 someText. 2) 鼠标点击 textbox1, 若单击点位于 someText 之内, 则 someText 被瞬间全选后所有的选择都被取消, 若单击点位于 someText 之外, 则不会发生任何事情, 没有任何选中的内容. 如下是解决办法 public partial class MainWindow : Windo…
 <Grid>         <TextBox Name="textBox1" PreviewTextInput="textBox1_PreviewTextInput"                   HorizontalAlignment="Stretch"   VerticalAlignment="Center"   />     </Grid> 1 2 3 4 5 6      …
因为在 IValueConverter 实现中,当文本不能转换为目标类型时返回 DependencyProperty.UnsetValue ,Validation.GetHasError 返回 true ,为何要绕一个圈让用户输入不能转换的文本,然后再获取错误状态呢?不如直接不让用户输入错误文本,于是写了一个 Behavior 派生类: using System; using System.Collections.Generic; using System.Linq; using System.…
用法:依赖属性 SelectTextOnFocus.Active = True public class SelectTextOnFocus : DependencyObject { public static readonly DependencyProperty ActiveProperty = DependencyProperty.RegisterAttached( "Active", typeof(bool), typeof(SelectTextOnFocus), new Pr…
TextBox双向绑定int属性,清空输入框时Setter方法未触发. 我的解决方案: 1.属性置为int?: 2.xmlns:sys="clr-namespace:System;assembly=mscorlib" 3.Text="{Binding Number,Mode=TwoWay,TargetNullValue={x:Static sys:String.Empty},UpdateSourceTrigger=PropertyChanged}"…
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="Share…
将 TextWrapping 属性设置为 Wrap 会导致输入的文本在到达 TextBox 控件的边缘时换至新行,必要时会自动扩展 TextBox 控件以便为新行留出空间. 将 AcceptsReturn 属性设置为 true 会导致在按 Return 键时插入新行,必要时会再次自动扩展 TextBox 以便为新行留出空间.…
<TextBox x:Name="xcontent" Text="sfasdf" Grid.Row="0" AcceptsReturn="True" TextWrapping="Wrap" Margin="1,1,1,1" KeyDown="content_key"/> private void content_key(object sender , Ke…
/// <summary> /// 选择日期 /// </summary> private void RQTxt_MouseWheel(object sender, MouseWheelEventArgs e) { if (XZQTxt.IsSelectionActive && XZQTxt.Text != null) Math(e.Delta, XZQTxt); } /// <summary> /// 输入月份更新文本 /// </summary…
1.设置textbox属性InputMethod.IsInputMethodEnabled="False" 2.增加KeyDown事件 private void TextBox_KeyDown(object sender, KeyEventArgs e) { #region --对输入中文就没办法屏蔽,可以在Textbox添加属性InputMethod.IsInputMethodEnabled="False"-- //屏蔽非法按键 if (e.Key >= K…
<Grid Background="Gray" Height="230" Width="229"> <Border CornerRadius="15" BorderBrush="White" BorderThickness="5" Margin="12,23,20,174" Background="White"> <T…