在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>
最新文章
政策新闻 | 抚顺市公共租赁住房申请保障资格公示(2025.7.18—2025.7.24) 政策新闻 | 培训强意识 演练砺技能 共筑安全线——云岩区消防救援大队贵开路消防救援站为贵阳市城建档案馆开展消防指导演练 政策新闻 | 深圳市住房保障署面向人才配售住房选房的通告(住保售〔2025〕016号批次) 政策新闻 | 庄劲松主持召开昌都市2025年林长制河湖长制工作会议 政策新闻 | 呼伦贝尔7月14日消防工作动态 加快推进我市消防历史遗留问题化解 政策新闻 | 2025年6月恩施州水质月报 政策新闻 | 广州市存量房交易资金托管让房产交易更安心放心 政策新闻 | 临沂住建局关于2025年度第十四批建筑施工企业安全生产许可证审核情况的公示 政策新闻 | 铜仁市援驰榕江灾后重建 政策新闻 | 襄阳市安全站:“豁免机制”激活绿色动能,打造蓝天发展新标杆