我需要将绑定值发送到ValidationRule
。我正在使用DependencyObject
,但其值始终为null
。ValidationRule和DependencyObject
这里是我的DependencyObject
public class MyDependencyObject : DependencyObject
{
public string BindingValue
{
get { return (string)GetValue(BindingValueProperty); }
set { SetValue(BindingValueProperty, value); }
}
public static readonly DependencyProperty BindingValueProperty =
DependencyProperty.Register("BindingValue", typeof(string), typeof(MyDependencyObject), new UIPropertyMetadata(null));
}
这里是我的ValidationRule
:
public class MyTextBoxValidationRule : ValidationRule
{
private MyDependencyObject _TxtVal;
public MyDependencyObject TxtVal
{
get { return _TxtVal; }
set { _TxtVal = value; }
}
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
//Validation Logic
}
}
这里是XAML
:
<TextBox >
<TextBox.Text>
<Binding Path="DataUserTypes"
NotifyOnValidationError="True"
ValidatesOnDataErrors="True"
Mode="TwoWay"
UpdateSourceTrigger="PropertyChanged"
NotifyOnSourceUpdated="True"
NotifyOnTargetUpdated="True"
Delay="100">
<Binding.ValidationRules>
<local:MyTextBoxValidationRule ValidatesOnTargetUpdated="True" >
<local:MyTextBoxValidationRule.TxtVal >
<local:MyDependencyObject
TxtVal="{Binding Path=ValidateValue}" />
</local:MyTextBoxValidationRule.TxtVal>
</local:MyTextBoxValidationRule>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
当我通过验证的MyTextBoxValidationRule
TxtVal.BindingValue
一步总是null
。我不知道为什么
编辑:
我改变DisplayValue
到DataUserTypes
因为似乎有一些混乱。
<Binding Path="DataUserTypes"
这是文本框的Text值的绑定。我需要根据酒店ValidateValue
验证DataUserTypes
。我试图用DependencyObject
TxtVal
来做到这一点。
我也修复了一个复制和粘贴类型。有一个叫TextValID
的字段应该是TxtVal
。对于那个很抱歉。
'<地方:MyDependencyObject TEXTVALID = “{绑定路径= ValidateValue}”/>' - 什么是TextValID这里? – mechanic
@ mechanic对不起,这是一个复制和粘贴错字。请参阅更新的编辑 – Xaphann