I'm sure there is a better way (please tell me!), But here one way that I combined works. There seems to be a simpler way. For the BaudRate property, use:
public int BaudRate { get { return m_baudRate; } set { if (value != m_baudRate) { m_baudRate = value; OnPropertyChanged("BaudRate");
For XAML, I don't have significant markup:
<TextBox Height="23" Margin="137,70,21,0" Name="textBox1" VerticalAlignment="Top" />
Now this is the dirty part ... Create a class to test:
public class IntRangeRule : ValidationRule { // See ValidationRule Class public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { try { if (value is int) // replace with your own logic or more robust handling... { return new ValidationResult(true, "Fine"); } else { return new ValidationResult(false, "Illegal characters or "); } } catch (Exception e) { return new ValidationResult(false, "Illegal characters or " + e.Message); } } }
And then in the constructor of Window1 (MainWindow):
Binding myBinding = new Binding("BaudRate"); myBinding.NotifyOnValidationError = true; myBinding.Mode = BindingMode.TwoWay; ValidationRule rule = new IntRangeRule(); myBinding.ValidationRules.Add(rule); myBinding.Source = m_data; // where m_data is the member variable of type ConfigurationData textBox1.SetBinding(TextBox.TextProperty, myBinding);
All my attempts to do everything in the markup failed. The best ways?
Dave
source share