Wpf UserControl在Silverlight中不显示验证异常
我有一个用户控件,并已将其依赖项属性TextValue绑定到视图模型 用户控制代码 My EntityViewModel.cs正在实现INotifyDataErrorInfo接口并继承ViewModel基类 ViewModelBase.cs实现INotifyPropertyChanged 当我将textbox或其他silverlight控件绑定到viewmodel时,我的代码工作正常。 并在控件上显示正确的验证异常 但是当用户控件获得验证异常时,该控件不显示任何异常Wpf UserControl在Silverlight中不显示验证异常,wpf,silverlight,user-controls,Wpf,Silverlight,User Controls,我有一个用户控件,并已将其依赖项属性TextValue绑定到视图模型 用户控制代码 My EntityViewModel.cs正在实现INotifyDataErrorInfo接口并继承ViewModel基类 ViewModelBase.cs实现INotifyPropertyChanged 当我将textbox或其他silverlight控件绑定到viewmodel时,我的代码工作正常。 并在控件上显示正确的验证异常 但是当用户控件获得验证异常时,该控件不显示任何异常 我没有发现用户控件有什么问题
我没有发现用户控件有什么问题。??如果没有用户控件的代码隐藏,很难完全调试该问题,但是,在我看来,您对代码隐藏有一个依赖属性,如下所示:
public static DependencyProperty TextValueProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(NumericUpDown1), null)
<TextBox Text="{Binding ElementName=View, Path=TextValue, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" ... />
然后,您似乎正在使用TextInputStart、KeyDown和KeyUp事件来捕获对底层控件的更改。我的直觉是:
a无法将TextValue属性更新为新值。
b您的代码隐藏正在干扰验证过程
我建议您在xaml中命名用户控件,而不是代码隐藏;i、 e
<UserControl x:Class="NumericUpDown1" x:Name="View"> ... </UserControl>
然后将基础TextBox的文本值直接绑定到dependency属性,如下所示:
public static DependencyProperty TextValueProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(NumericUpDown1), null)
<TextBox Text="{Binding ElementName=View, Path=TextValue, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" ... />
这将允许验证机制正常进行
希望有帮助。您所说的“当用户控件出现验证异常”是什么意思?
public static DependencyProperty TextValueProperty = DependencyProperty.Register("TextValue", typeof(string), typeof(NumericUpDown1), null)
<UserControl x:Class="NumericUpDown1" x:Name="View"> ... </UserControl>
<TextBox Text="{Binding ElementName=View, Path=TextValue, Mode=TwoWay, ValidatesOnNotifyDataErrors=True, NotifyOnValidationError=True}" ... />