我正在尝试在WPF应用程序(这是Solidworks的插件)中实现输入验证。我使用此示例作为参考,我相信我已正确应用它-https://developingfor.net/2009/10/13/using-custom-validation-rules-in-wpf/。我代码中的规则应检查文本框中的输入是否为整数。但是,由于某些原因,验证检查不会触发。我在代码中放置了断点,但它从未运行过验证功能。我的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);
}
}
}
编译器不会引发任何错误,并且当我在该文本框中写入任何内容时,验证规则将不起作用。从未达到Validate()
。我的错误在哪里?
答案 0 :(得分:1)
您提供的示例代码对我来说很好用,我在ValidationRule
中遇到了断点。
我使用此MainWindow
代码测试了Binding
:
<Window x:Class="SolidworksPlugin.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="window"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
Topmost="True"
RenderTransformOrigin="0 0">
<Grid>
<MyAddInControl></MyAddInControl>
</Grid>
</Window>
和下面的示例代码创建一个ViewModel:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
DataContext = new MyViewModel();
}
}
public class MyViewModel {
public int Int1 { get; set; }
}
如果ValidationRule
本身无法正确解析,则不会调用Binding
,因此请检查您的视图模型是否确实具有名为Int1
的属性以及{{1 }}正确设置为该视图模型。