ValidationRule WPF的附加属性或依赖属性 [英] Attached or dependecy Property for ValidationRule WPF
问题描述
我想将xaml中的附加属性或依赖项属性绑定到xaml中的ValidationRule,然后基于附加属性或依赖项属性的值,我要在Validation规则中做出总和决策。我找不到任何解决方案
如何将可绑定值传递给验证规则。
我为您提供一个示例代码可以帮助您。我已经定义了ValidationRule来验证texbox用户输入。验证类型根据一个枚举参数的值执行。可用的验证类型为:用户输入不能为空,用户输入必须为数字,用户输入必须为IP地址。第二个参数允许特定显示警告消息。如您所知,用于绑定目的的变量应该是DependendyProperty,所以在这里您可以找到带有参数声明的类。
公共类ValidationParams: DependencyObject
{
//依赖属性
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register( Message,
typeof(string),
typeof(ValidationParams),
新的FrameworkPropertyMetadata(string.Empty));
公共静态只读DependencyProperty ValidationTypeProperty = DependencyProperty.Register( ValidationType,
typeof(FieldValidationRule.EnmValidationType),
typeof(ValidationParams),
new FrameworkPropertyMetadata(FieldValidationRule .EnmValidationType.FieldNotEmpty));
//属性
[Category( Message)]
公共字符串消息
{
get {return(string)GetValue(MessageProperty); }
set {SetValue(MessageProperty,value); }
}
[Category( ValidationType)]
public FieldValidationRule.EnmValidationType ValidationType
{
get {return(FieldValidationRule.EnmValidationType)GetValue( ValidationTypeProperty); }
set {SetValue(ValidationTypeProperty,value); }
}
然后是validaterule类:
公共类FieldValidationRule:ValidationRule
{
公共枚举EnmValidationType
{
FieldNotEmpty,
FieldNumeric,
FieldIPAddress
}
//局部变量和对象
private ValidationParams mParams = new ValidationParams();
public ValidationParams Params
{
get {return mParams; }
set {mParams = value; }
}
//重写
公共重写ValidationResult Validate(对象值,System.Globalization.CultureInfo cultureInfo)
{
ValidationResult objResult = null;
string sValue =字符串形式的值;
objResult = new ValidationResult(true,null);
开关(Params.ValidationType)
{
case EnmValidationType.FieldNotEmpty:
if(string.IsNullOrEmpty(sValue)== true)
objResult = new ValidationResult(false ,Params.Message);
休息时间;
case EnmValidationType.FieldNumeric:
int iValue = 0;
if(int.TryParse(sValue,out iValue)==否)
objResult = new ValidationResult(false,Params.Message);
休息时间;
情况EnmValidationType.FieldIPAddress:
IPAddress objValue = IPMatrix.CreateHostAddr();
if(IPAddress.TryParse(sValue,out objValue)== false)
objResult = new ValidationResult(false,Params.Message);
休息时间;
}
返回objResult;
}
}
最后是XAML代码:
< TextBox Style = {DynamicResource FieldValue} Grid.Column = 1 IsReadOnly = False>
< TextBox.Text>
< Binding Source = {StaticResource XmlItemChannel} XPath = @ Name Mode = TwoWay UpdateSourceTrigger = LostFocus>
< Binding.ValidationRules>
< data:FieldValidationRule>
< data:FieldValidationRule.Params>
< data:ValidationParams消息= {DynamicResource ERR002} ValidationType = FieldNotEmpty />
< / data:FieldValidationRule.Params>
< / data:FieldValidationRule>
< /Binding.ValidationRules>
< / Binding>
< /TextBox.Text>
< / TextBox>
您可以看到Message参数已绑定到资源,但是您也可以经典地将其绑定。 / p>
I want to bind the attached property or dependency property in xaml for the ValidationRule in xaml and then based on the value of the attached property or dependency property I want to make sum decision in the Validation rule. I can't find any solution how can I Pass bindable value to the Validation Rule.
I supply you a sample code to help you. I have defined a ValidationRule to validate a texbox user input. The type of validation is performed according value of one enum parameter. Type of validation available are: user input cannot be empty, user input must be numeric, user input must be an IP address. A second parameter allows to specificy warning message displayed. As you know a variable for binding purposes should be a DependendyProperty, so here you find class with paramaters declaration.
public class ValidationParams : DependencyObject
{
// Dependency Properties
public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message",
typeof(string),
typeof(ValidationParams),
new FrameworkPropertyMetadata(string.Empty));
public static readonly DependencyProperty ValidationTypeProperty = DependencyProperty.Register("ValidationType",
typeof(FieldValidationRule.EnmValidationType),
typeof(ValidationParams),
new FrameworkPropertyMetadata(FieldValidationRule.EnmValidationType.FieldNotEmpty));
// Properties
[Category("Message")]
public string Message
{
get { return (string)GetValue(MessageProperty); }
set { SetValue(MessageProperty, value); }
}
[Category("ValidationType")]
public FieldValidationRule.EnmValidationType ValidationType
{
get { return (FieldValidationRule.EnmValidationType)GetValue(ValidationTypeProperty); }
set { SetValue(ValidationTypeProperty, value); }
}
Then here is the validationrule class:
public class FieldValidationRule : ValidationRule
{
public enum EnmValidationType
{
FieldNotEmpty,
FieldNumeric,
FieldIPAddress
}
// Local variables and objects
private ValidationParams mParams = new ValidationParams();
public ValidationParams Params
{
get { return mParams; }
set { mParams = value; }
}
// Override
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
ValidationResult objResult = null;
string sValue = value as string;
objResult = new ValidationResult(true, null);
switch (Params.ValidationType)
{
case EnmValidationType.FieldNotEmpty:
if(string.IsNullOrEmpty(sValue) == true)
objResult = new ValidationResult(false, Params.Message);
break;
case EnmValidationType.FieldNumeric:
int iValue = 0;
if(int.TryParse(sValue, out iValue) == false)
objResult = new ValidationResult(false, Params.Message);
break;
case EnmValidationType.FieldIPAddress:
IPAddress objValue = IPMatrix.CreateHostAddr();
if(IPAddress.TryParse(sValue, out objValue) == false)
objResult = new ValidationResult(false, Params.Message);
break;
}
return objResult;
}
}
And finally here is the XAML code:
<TextBox Style="{DynamicResource FieldValue}" Grid.Column="1" IsReadOnly="False">
<TextBox.Text>
<Binding Source="{StaticResource XmlItemChannel}" XPath="@Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
<Binding.ValidationRules>
<data:FieldValidationRule>
<data:FieldValidationRule.Params>
<data:ValidationParams Message="{DynamicResource ERR002}" ValidationType="FieldNotEmpty" />
</data:FieldValidationRule.Params>
</data:FieldValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
You can see that parameter Message is binded to a resource, but you can classically bind it too.
这篇关于ValidationRule WPF的附加属性或依赖属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!