你可以有內部VR公共屬性,它可以同時宣告這樣的驗證規則進行設置:
public class VR : ValidationRule
{
public VR()
{
}
public static readonly DependencyProperty SetSelectedValueProperty=
DependencyProperty.Register("SetSelectedValue", typeof(string), typeof(VR), new
PropertyMetadata("", new PropertyChangedCallback(OnSetSelectedValuePropertyChanged)));
public string SetSelectedValue {
get { return (string)GetValue(SetSelectedValueProperty); }
set { SetValue(SetSelectedValueProperty, value); }
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
}
private static void OnSetSelectedValuePropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e) {
// Some code
}
}
設置在XAML中的SelectedValue是這樣的:
<Binding.ValidationRules>
<e:VR SetSelectedValue="{Binding ElementName="ComboBox1", Path=SelectedValue.Content}"/>
</Binding.ValidationRules>
只是猜測這裏,但可你添加一個依賴項屬性到你的ValidationRule並將它綁定到你的ComboBox的SelectedValue? – Mark