在WPF中使用数据绑定,如果用户输入和绑定类型转换失败,控件就会显示出现错误的模板,
比如一个Textbox绑定到一个int 属性,如果用户输入一个string,那这个textbox就会显示错误模板,一般会是在TextBox外显示红线,
当然这个模板也可以自己设置。那如果这个界面有一个确定Button,我想实现TextBox里输入非数字和数字值小于0时Button都不可用,
那该怎么实现呢?
namespace WpfApplication6
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new ViewModel(this);
}
}
public class ViewModel : INotifyPropertyChanged
{
private Window win = null;
private int errors = 0;
private int num1 = 0;
public int Num1
{
get
{
return num1;
}
set
{
num1 = value;
if (num1 < 0)
{
throw new ArgumentException("值太小!");
}
}
}
private int num2 = 0;
public int Num2
{
get
{
return num2;
}
set
{
num2 = value;
if (num2 > 0)
{
throw new ArgumentException("值太大!");
}
}
}
public ICommand OK_Command
{
get
{
return new ReLayCommand(() => {
},()=> {
return errors == 0;
});
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnRaisePropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public ViewModel(Window win)
{
this.win = win;
Validation.AddErrorHandler(win, ErrorHandler);
}
private void ErrorHandler(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
{
errors += 1;
}
if (e.Action == ValidationErrorEventAction.Removed)
{
errors -= 1;
}
OnRaisePropertyChanged("OK_Command");
}
}
public class ReLayCommand : ICommand
{
private Action _execute = null;
private Func<bool> _canExecute = null;
public event EventHandler CanExecuteChanged;
public ReLayCommand(Action _execute, Func<bool> _canExecute = null)
{
this._execute = _execute;
this._canExecute = _canExecute;
}
public bool CanExecute(object parameter)
{
if (_canExecute != null)
return _canExecute();
return true;
}
public void Execute(object parameter)
{
if (_execute != null)
_execute();
}
}
}
<Window x:Class="WpfApplication6.MainWindow"
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"
xmlns:local="clr-namespace:WpfApplication6"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="TextBox">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="140,76,0,0" TextWrapping="Wrap" Text="{Binding Path=Num1,UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True,ValidatesOnExceptions=True}" VerticalAlignment="Top" Width="120"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="23" Margin="140,160,0,0" TextWrapping="Wrap" Text="{Binding Path=Num2,UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True,ValidatesOnExceptions=True}" VerticalAlignment="Top" Width="120"/>
<Button x:Name="button" Command="{Binding Path=OK_Command}" Content="Button" HorizontalAlignment="Left" Margin="129,239,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>
</Window>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
2017-02-28 jQuery实现限制文本框的输入长度
2017-02-28 js与jquery实时监听输入框值的oninput与onpropertychange方法
2017-02-28 jQuery实现的浮动层div浏览器居中显示效果
2017-02-28 jquery代码规范让代码越来越好看
2017-02-28 asp.net截屏功能实现截取web页面
2017-02-28 拦截asp.net输出流并进行处理的方法
2017-02-28 asp.net调用系统设置字体文本框的方法