C#WPF自定义验证规则不&';t火

C#WPF自定义验证规则不&';t火,c#,wpf,C#,Wpf,我试图在我的WPF应用程序(Solidworks的一个插件)中实现输入验证。我使用这个例子作为参考,我相信我正确地应用了它-。我的代码中有一个规则应该检查文本框中的输入是否为整数。然而,由于某些原因,验证检查没有启动。我在代码中设置了断点,它从不运行验证函数。我的WPF的相关部分: <UserControl x:Class="SolidworksPlugin.MyAddInControl" xmlns="http://schemas.microsoft.com/

我试图在我的WPF应用程序(Solidworks的一个插件)中实现输入验证。我使用这个例子作为参考,我相信我正确地应用了它-。我的代码中有一个规则应该检查文本框中的输入是否为整数。然而,由于某些原因,验证检查没有启动。我在代码中设置了断点,它从不运行验证函数。我的WPF的相关部分:

<UserControl x:Class="SolidworksPlugin.MyAddInControl" 
             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:local="clr-namespace:Solidworks_Plugin"
             xmlns:ValidationRules="clr-namespace:CustomValidationRules"
             mc:Ignorable="d" 
             Height="600" Width="300">

    <StackPanel>
        <TextBox
            x:Name="Int1TextBox"
            VerticalAlignment="Center"
            HorizontalAlignment="Center"
            MinWidth="50"
            Margin="0,0,6,0">
            <Binding
                Path="Int1"
                UpdateSourceTrigger="PropertyChanged"
                Mode="TwoWay">
                <Binding.ValidationRules>
                    <ValidationRules:IntegerValidationRule
                        Min="1"
                        Max="9999999"
                        FieldName="Int1" 
                    />
                </Binding.ValidationRules>
            </Binding>
        </TextBox>
    </StackPanel>

</UserControl>

以及此WPF为验证规则引用的InputValidator类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;

namespace CustomValidationRules
{
    public class IntegerValidationRule : ValidationRule
    {
        private int _min = int.MinValue;
        private int _max = int.MaxValue;
        private string _fieldName = "Field";
        private string _customMessage = String.Empty;

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

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

        public string FieldName
        {
            get { return _fieldName; }
            set { _fieldName = value; }
        }

        public string CustomMessage
        {
            get { return _customMessage; }
            set { _customMessage = value; }
        }


        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            int num = 0;

            if (!int.TryParse(value.ToString(), out num))
                return new ValidationResult(false, String.Format("{0} must contain an integer value.", FieldName));

            if (num < Min || num > Max)
            {
                if (!String.IsNullOrEmpty(CustomMessage))
                    return new ValidationResult(false, CustomMessage);


                return new ValidationResult(false, String.Format("{0} must be between {1} and {2}.",
                                           FieldName, Min, Max));
            }

            return new ValidationResult(true, null);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows.Controls;
命名空间CustomValidationRules
{
公共类IntegerValidationRule:ValidationRule
{
私有int _min=int.MinValue;
私有int _max=int.MaxValue;
私有字符串_fieldName=“Field”;
私有字符串_customMessage=string.Empty;
公共int Min
{
获取{return\u min;}
设置{u min=value;}
}
公共整数最大值
{
获取{return\u max;}
设置{u max=value;}
}
公共字符串字段名
{
获取{return\u fieldName;}
设置{u fieldName=value;}
}
公共字符串自定义消息
{
获取{return\u customMessage;}
设置{u customMessage=value;}
}
公共覆盖验证结果验证(对象值,System.Globalization.CultureInfo CultureInfo)
{
int num=0;
如果(!int.TryParse(value.ToString(),out num))
返回新的ValidationResult(false,String.Format(“{0}必须包含整数值。”,FieldName));
如果(numMax)
{
如果(!String.IsNullOrEmpty(CustomMessage))
返回新的ValidationResult(false,CustomMessage);
返回新的ValidationResult(false,String.Format(“{0}必须介于{1}和{2}之间)。”,
字段名,最小值,最大值);
}
返回新的ValidationResult(true,null);
}
}
}

编译器不会抛出任何错误,当我将任何内容写入该文本框时,验证规则不起作用。从未到达
Validate()
。我的错误在哪里?

您作为示例提供的代码对我很好,我在
验证规则中遇到了断点

我使用这个
main窗口
代码来测试
绑定


下面是创建ViewModel的示例代码:

公共部分类主窗口:窗口{
公共主窗口(){
初始化组件();
DataContext=新的MyViewModel();
}
}
公共类MyViewModel{
公共int Int1{get;set;}
}

如果
绑定本身没有正确解析,则不会调用
验证规则
,因此,请检查您的视图模型是否实际具有名为
Int1
的属性,以及对象的
DataContext
是否正确设置为该视图模型。

您能否将XAML示例减少到严格的最小值,以便我们可以轻松地自己尝试它?我减少了它。谢谢。能否显示创建MyAddInControl的XAML代码以及关联视图模型的代码?MyAddInControl在TaskpaneHostUI类中初始化:private void InitializeComponent(){this.MyAddInControl L1=new SolidworksPlugin.MyAddInControl();}}。我不确定你所说的“关联视图模型的代码”是什么意思,你能说得更具体一点吗?你所说的“视图模型”是指用户控件吗?文本框名为Int1Textbox,我将其更改为Int1,但它仍然无法工作。我不太明白你指的是哪个对象。当你用XAML在绑定中写入
Path=“Int1”
时,你指的是哪个属性
Int1
?它必须是一个对象的属性,看看我在示例代码中是如何做到这一点的。我开始理解了,但还不完全清楚。我的代码中没有MyViewModel或任何类似的类。用户控件在TaskpaneHostUI类中初始化,如下所示:publicTaskPaneHostUI(){InitializeComponent();}。DataContext从来没有设置过,我不知道应该将其设置为什么(除了没有它的验证外,其他一切都可以使用)。您应该创建一个像my
MyViewModel
类这样的对象,并将其设置为
DataContext
,因为
绑定
只适用于
DataContext
集。我建议您阅读更多关于WPF和MVVM模式中绑定的内容。这非常复杂。我在你的例子中尝试过,但似乎没有任何效果。我会按照你的建议读一些书。