Binding 値を に送信する必要があります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 をステップ実行すると、MyTextBoxValidationRule
TxtVal.BindingValue
常にnull
. 理由がわかりません
編集:
混乱しているように見えるので、に変更DisplayValue
しました。DataUserTypes
<Binding Path="DataUserTypes"
これは、テキスト ボックスの Text 値のバインディングです。DataUserTypes
プロパティに基づいて検証する必要がありますValidateValue
。でそれをやろうとしていDependencyObject
TxtVal
ます。
コピー&ペーストタイプも修正しました。TextValID
あるはずの というフィールドがありましたTxtVal
。申し訳ありません。