下載app免費領(lǐng)取會員
在WPF中使用數(shù)據(jù)綁定,如果用戶輸入和綁定類型轉(zhuǎn)換失敗,控件就會顯示出現(xiàn)錯誤的模板,
比如一個Textbox綁定到一個int 屬性,如果用戶輸入一個string,那這個textbox就會顯示錯誤模板,一般會是在TextBox外顯示紅線,
當(dāng)然這個模板也可以自己設(shè)置。那如果這個界面有一個確定Button,我想實現(xiàn)TextBox里輸入非數(shù)字和數(shù)字值小于0時Button都不可用,
那該怎么實現(xiàn)呢?
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>
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。
上一篇:二次開發(fā)教程:Revit開發(fā)之調(diào)用Revit命令的另一種方法
下一篇:二次開發(fā)教程:Revit開發(fā)之多線程
推薦專題