簡體   English   中英

WPF:找不到自定義驗證規則

[英]WPF: Custom Validation Rule not found

所以我嘗試在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”,我在做什么錯?

您缺少名稱空間導入 將以下xmlns:l添加到您的Window聲明中,為您自己更改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"

您需要添加對存在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" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM