有效值后的ValidationRule仍显示错误消息

时间:2015-05-21 05:54:24

标签: c# wpf

我构建了一个如下所示的usercontrol:

enter image description here

xaml代码

<UserControl x:Class="Customizing.Views.IpRangeFields"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ig="http://schemas.infragistics.com/xaml"
             xmlns:local="clr-namespace:Customizing.Views"
             mc:Ignorable="d"
             x:Name="_parent"
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>

    </UserControl.Resources>
    <Grid DataContext="{Binding ElementName=_parent}">
        <Grid.RowDefinitions>
            <RowDefinition Height="60" />
            <RowDefinition Height="10" />
            <RowDefinition Height="60" />
            <RowDefinition Height="10" />
            <RowDefinition Height="60" />
            <RowDefinition Height="10" />
            <RowDefinition Height="60" />
            <RowDefinition Height="10" />
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2.5*" />
        </Grid.ColumnDefinitions>

        <Label Grid.Row="0" Grid.Column="0" Content="Start" VerticalContentAlignment="Center" FontSize="18" HorizontalContentAlignment="Center" FontWeight="Bold"/>
        <Label Grid.Row="2" Grid.Column="0" Content="End" VerticalContentAlignment="Center" FontSize="18" HorizontalContentAlignment="Center" FontWeight="Bold"/>
        <Label Grid.Row="4" Grid.Column="0" Content="Subnet" VerticalContentAlignment="Center" FontSize="18" HorizontalContentAlignment="Center" FontWeight="Bold"/>
        <Label Grid.Row="6" Grid.Column="0" Content="Gateway" VerticalContentAlignment="Center" FontSize="18" HorizontalContentAlignment="Center" FontWeight="Bold"/>

        <ig:XamMaskedInput x:Name="Start" Mask="###-###-###-###" DisplayMode="IncludeBoth" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Row="0" Grid.Column="1" FontSize="22">
            <Binding Path="IpAddr" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <local:IpAddressRule ValidationStep="RawProposedValue"/>
                </Binding.ValidationRules>
            </Binding>
        </ig:XamMaskedInput>
        <ig:XamMaskedInput x:Name="End" Mask="###-###-###-###" DisplayMode="IncludeBoth" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Row="2" Grid.Column="1" FontSize="22"/>
        <ig:XamMaskedInput x:Name="Subnet" Mask="###-###-###-###" DisplayMode="IncludeBoth" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Row="4" Grid.Column="1" FontSize="22"/>
        <ig:XamMaskedInput x:Name="Gateway" Mask="###-###-###-###" DisplayMode="IncludeBoth" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Row="6" Grid.Column="1" FontSize="22"/>

    </Grid>
</UserControl>

部分代码和验证规则子类:

using System;
using System.Diagnostics;
using System.Globalization;
using System.Windows.Controls;
using System.Windows.Input;
using Infragistics.Controls.Editors;
using System.Net;
using System.Windows;

namespace Customizing.Views
{
    /// <summary>
    ///     Interaction logic for IpRangeFields.xaml
    /// </summary>
    public partial class IpRangeFields : UserControl
    {

        public string IpAddr
        {
            get { return (string)GetValue(IpAddrProperty); }
            set { SetValue(IpAddrProperty, value); }
        }

        public static readonly DependencyProperty IpAddrProperty = DependencyProperty.Register("IpAddr", typeof(string), typeof(IpRangeFields), new PropertyMetadata(null)); 

        public IpRangeFields()
        {
            InitializeComponent();
        }
    }

    public class IpAddressRule : ValidationRule
    {
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {

            try
            {
                string addr = value.ToString().Replace("-", ".");
                var ip = IPAddress.Parse(addr);
                return new ValidationResult(true, null);
            }
            catch (Exception ex)
            {
                return new ValidationResult(false, ex.Message);
            }
        }
    }

}

我的问题是,如果我输入有效的IP无关紧要,错误信息仍然显示,为什么? enter image description here

0 个答案:

没有答案