捕获WPF隐式转换错误

时间:2011-03-08 11:39:13

标签: validation exception binding

我有一个绑定到整数属性的WPF TextBox。如果我在文本框中输入任何字母,文本框会在失去焦点时显示红色边框,表示转换错误。到目前为止一切都很好。

我想在代码中捕获转换错误。我认为错误来自转换异常,并且在这些情况下,文本框绑定对象上的ExceptionValidationRule将无法通过验证。但是,情况似乎并非如此。

在下面的示例中,我会在输入文本字符串时将背景视为橙色,然后单击按钮,但这不会发生。

XAML:

<Window x:Class="ValidationTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ValidationTest="clr-namespace:ValidationTest" 
    Title="MainWindow" Height="89" Width="239">
<Grid>
    <TextBox Height="27" HorizontalAlignment="Left" Margin="12,12,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" >
        <TextBox.Text>
            <Binding Path="IntegerField">
                <Binding.ValidationRules>
                    <ExceptionValidationRule />
                    <!--ValidationTest:IntegerValidationRule /-->
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <Button Content="Test" Height="27" HorizontalAlignment="Left" Margin="146,12,0,0" Name="button1" VerticalAlignment="Top" Width="60" Click="button1_Click" />
</Grid>

代码背后:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Globalization;

namespace ValidationTest {
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public int IntegerField { get; set; }

        public MainWindow() {
            InitializeComponent();
            this.DataContext = this;
        }

        private void button1_Click(object sender, System.Windows.RoutedEventArgs e) {
            // Validate all the bindings on the textbox 
            bool valid = true;
            LocalValueEnumerator localValues = textBox1.GetLocalValueEnumerator();
            while (localValues.MoveNext()) {
                LocalValueEntry entry = localValues.Current;
                if (BindingOperations.IsDataBound(textBox1, entry.Property)) {
                    Binding binding = BindingOperations.GetBinding(textBox1, entry.Property);
                    foreach (ValidationRule rule in binding.ValidationRules) {
                        ValidationResult result = rule.Validate(textBox1.GetValue(entry.Property), null);
                        if (!result.IsValid) {
                            valid = false;
                        }
                    }
                }
            }
            this.Background = valid ? Brushes.Green : Brushes.Orange;
        }

    }

    public class IntegerValidationRule : ValidationRule {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
            int val;
            if (!int.TryParse(value as string, out val)) {
                return new ValidationResult(false, "NAN");
            }
            return ValidationResult.ValidResult;
        }
    }
}

我可以通过在XAML中取消注释自定义IntegerValidationRule来实现示例中所需的功能......但这不适用于我的“真实”应用程序,因为文本框绑定的数据类型在设计时是未知的。

有人知道解决方案吗?

0 个答案:

没有答案