我想創建一個給定的控件(在這種情況下,它是文本框)的驗證規則。WPF DependencyProperty驗證綁定到對象屬性
儘管採取了適當的步驟,但我無法獲得成功綁定到對象的屬性:ValidationRule和DepedencyProperty被利用。
請在下面找到代碼。值得注意的是,自定義Validation類中的「Is Required」始終爲False,除非我明確地在XAML中設置了值(根據「Is Ranged」參數,沒有綁定)。
任何提示和建議表示讚賞。
預先感謝您:)
XAML代碼:
<TextBox Style="{StaticResource ValidationError}" LostFocus="ForceValidationCheck"
Visibility="{Binding Type, Converter={StaticResource Visibility}, ConverterParameter='Number'}"
IsEnabled="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource IsEnabled}}">
<TextBox.Text>
<Binding Path="Value">
<Binding.ValidationRules>
<validation:NumericValidation>
<validation:NumericValidation.Dependency>
<validation:NumericDependency IsRequired="{Binding Path=IsRequired}" IsRanged="True" Min="5"/>
</validation:NumericValidation.Dependency>
</validation:NumericValidation>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
校驗類:
public NumericDependency Dependency { get; set; }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
isRequired = Dependency.IsRequired;
}
驗證相關性類:
public static readonly DependencyProperty IsRequiredProperty =
DependencyProperty.Register("IsRequired", typeof(bool), typeof(NumericDependency), new UIPropertyMetadata(default(bool)));
public bool IsRequired
{
get
{
return (bool) GetValue(IsRequiredProperty);
}
set
{
SetValue(IsRequiredProperty, value);
}
}
鏈接已死亡。 – Endery