WPF:找不到自定义验证规则

时间:2014-01-19 18:02:32

标签: c# wpf

所以我尝试在WPF上设置自定义数字验证:

public class NumberValidation : ValidationRule {
    private int _min;
    private int _max;

    public NumberValidation() {
    }

    public int Min {
        get { return _min; }
        set { _min = value; }
    }

    public int Max {
        get { return _max; }
        set { _max = value; }
    }

    public override ValidationResult Validate(object value, CultureInfo cultureInfo) {
        int age = 0;

        try {
            if (((string)value).Length > 0)
                age = Int32.Parse((String)value);
        } catch (Exception e) {
            return new ValidationResult(false, "Illegal characters or " + e.Message);
        }

        if ((age < Min) || (age > Max)) {
            return new ValidationResult(false,
              "Please enter an age in the range: " + Min + " - " + Max + ".");
        } else {
            return new ValidationResult(true, null);
        }
    }
}

然后尝试在XML中实现它:

<Window x:Class="MiningControl.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" 
        DataContext="{Binding}">
    <Grid>
        <TextBox Height="23" HorizontalAlignment="Right" Margin="0,74,161,0" Name="txb_idleTime" VerticalAlignment="Top" Width="120"
            >
            <TextBox.Text>
                <Binding Path="MinIdleTime" UpdateSourceTrigger="PropertyChanged" >
                    <Binding.ValidationRules>
                        <NumberValidation Min="10" Max="3600" />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Label Content="Minimum Idle Time (min):" Height="28" HorizontalAlignment="Left" Margin="23,72,0,0" Name="label1" VerticalAlignment="Top" Width="202" HorizontalContentAlignment="Right" />
    </Grid>
</Window>

但它不起作用它继续说XML命名空间中不存在“NumberValidation”,我做错了什么?

2 个答案:

答案 0 :(得分:1)

您错过了namespace import。将以下xmlns:l添加到您的窗口声明中,为您自己更改YourAppNamespace

<Window x:Class="MiningControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:YourAppNamespace"
    Title="MainWindow" Height="350" Width="525" 
    DataContext="{Binding}">

然后在NumberValidation前面声明名称空间,在本例中为l

<l:NumberValidation Min="10" Max="3600" />

请注意,如果NumberValidation位于不同的程序集中,则需要指定它,例如:

xmlns:l="clr-namespace:VendorControlLibrary;assembly=VendorControlLibrary"

答案 1 :(得分:0)

您需要添加对NumberValidation存在的命名空间的引用。

例如:

<Window x:Class="MiningControl.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:numberValidation="clr-namespace:TheAppsNamespace"
    Title="MainWindow" Height="350" Width="525" 
    DataContext="{Binding}">

然后你可以像这样使用它:

<numberValidation:NumberValidation Min="10" Max="3600" />