我需要将绑定值发送到ValidationRule
. 我正在使用DependencyObject
,但值始终是null
。
这是我的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>
当我通过 Validate in 时MyTextBoxValidationRule
TxtVal.BindingValue
总是null
. 我不知道为什么
编辑:
我改为DisplayValue
因为DataUserTypes
似乎有些混乱。
<Binding Path="DataUserTypes"
那是文本框的 Text 值的绑定。我需要DataUserTypes
根据属性进行验证ValidateValue
。我正在尝试使用DependencyObject
TxtVal
.
我还修复了复制和粘贴类型。有一个字段TextValID
应该是TxtVal
. 对于那个很抱歉。