WPF验证无效,Validation.ErrorTemplate未在intellisense中显示

时间:2014-10-13 19:00:27

标签: c# wpf validation xaml

我有一个XAML如下。不遵循MVVM进行测试。现在,当文本框焦点松动或属性发生变化时,或者无论是否发生异常,也不会出现文本框的红色边框。同样对于文本框,当我说Validation.ErrorTemplate属性没有在intellisense中显示时,一个蓝色的杂文发生说XAML中有错误。我怎样才能使它发挥作用?

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Applcation5"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <TextBox x:Name="txtdemo" Height="40" Margin="218,140,79,140">
            <TextBox.Text>
                <Binding Path="text" Mode="TwoWay" ValidatesOnDataErrors="True" ValidatesOnExceptions="True">
                    <Binding.ValidationRules>
                        <ExceptionValidationRule/>
                        <local:NameVaildation/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>
        <Button Height="20" VerticalAlignment="Bottom" Margin="253,0,152,61" RenderTransformOrigin="0.857,5"/>
        <TextBox Height="40" Width="70" Margin="162,239,285,41"></TextBox>
    </Grid>
</Window>

我的MainWindow.cs

public partial class MainWindow : Window
{
    private string _Text;
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public string text
    {
        get { return _Text; }
        set
        {
            _Text = value;
            if (value == null)
            {
                throw new ApplicationException("Name is null");
            }
        }
    }
}

最后NameVaildation课程:

public class NameVaildation : ValidationRule
{
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        string textvalue = value.ToString();

        if (textvalue == null)
        {
            return new ValidationResult(false, "Value cannot be null");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
}

0 个答案:

没有答案