Nie wiem czy to idealne rozwiązanie bo w WPF od czasów studiów nic nie pisałem ale przykład:
WPF:
<Grid>
<TextBox HorizontalAlignment="Left" Height="23" Margin="10,10,0,0" TextWrapping="Wrap" Text="{Binding NumberOfPeriods, Mode=TwoWay,
ValidatesOnExceptions=true, NotifyOnValidationError=true}" VerticalAlignment="Top" Width="120"/>
</Grid>
C#:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new Foo();
}
}
public class Foo : INotifyPropertyChanged
{
private int _numberOfPeriods;
[Range(1, int.MaxValue, ErrorMessage = @"Period recurrence should be positive.")]
public int NumberOfPeriods
{
get { return _numberOfPeriods; }
set
{
if (value == _numberOfPeriods) return;
Validator.ValidateProperty(value,
new ValidationContext(this, null, null) {MemberName = "NumberOfPeriods"});
_numberOfPeriods = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}