下载app免费领取会员
在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>
本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。