Silverlight 4.0 为什么可以';无法在validationSummary中显示验证错误?
我有一个表单,在实体元数据类中设置了一些验证。然后通过VM将实体实例绑定到UI。如下所示: Xaml-like:Silverlight 4.0 为什么可以';无法在validationSummary中显示验证错误?,silverlight-4.0,validation,wcf-ria-services,Silverlight 4.0,Validation,Wcf Ria Services,我有一个表单,在实体元数据类中设置了一些验证。然后通过VM将实体实例绑定到UI。如下所示: Xaml-like: <Grid x:Name="LayoutRoot"> <StackPanel VerticalAlignment="Top"> <input:ValidationSummary /> </StackPanel> <TextBox Te
<Grid x:Name="LayoutRoot">
<StackPanel VerticalAlignment="Top">
<input:ValidationSummary />
</StackPanel>
<TextBox Text="{Binding Name, Mode=TwoWay}" />
<ComboBox x:Name="xTest" ItemsSource="{Binding MyList}"
SelectedItem="{Binding MyItem,Mode=TwoWay,
DisplayMemberPath="MyName"
ValidatesOnDataErrors=True,
ValidatesOnNotifyDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True,UpdateSourceTrigger=Explicit}" />
</Grid>
但在运行应用程序时,我没有在ValuadationSummary中显示任何内容。
对于Combox,即使存在错误,看起来BindingValidationError事件也不会被触发。
如何解决此问题?为什么要使用显式UpdateSourceRigger 当绑定更新源对象时,Silverlight验证在绑定框架内进行。按照这种方式,不会出现绑定验证错误,因为您从未告诉绑定更新源对象。实际上是这样,但它发生在验证错误事件处理程序中。您已经编写了鸡和蛋代码
- 删除绑定上的
,或将其设置为更新资源触发器
李>默认值
- 删除对
的显式调用李>BindingExpression.UpdateSource
- 删除将组合框前景设置为红色-您使用的是
,这样就无需手动为控件着色李>NotifyOnValidationError=True
- 从绑定中删除
DisplayMemberPath
<Grid x:Name="LayoutRoot">
<StackPanel VerticalAlignment="Top">
<input:ValidationSummary />
<ComboBox x:Name="xTest" ItemsSource="{Binding MyList}"
SelectedItem="{Binding MyItem,
Mode=TwoWay,
ValidatesOnDataErrors=True,
ValidatesOnNotifyDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True}" />
</StackPanel>
</Grid>
非常感谢。完成你的建议:1。应保留DisplayMemberPath,因为MyName是实体MyItem的一个属性。2.在xaml中添加所有验证选项。3.删除事件BindingValidationError的隐藏代码。然后再次运行应用程序。结果是:我可以在VM中的Entity.ValidationErrors中获取验证错误(我使用消息框显示它以进行测试),但在ValidationSummary中无法获取它。在您的示例中,绑定中有DisplayMemberPath。绑定标记扩展中没有DisplayMemberPath选项。若要使验证摘要正常工作,请将其移动到与组合框相同的父容器中(已编辑我的答案)。
[Required]
public string Name { get; set; }
[RequiredAttribute]
public int MyItemID { get; set; }
<Grid x:Name="LayoutRoot">
<StackPanel VerticalAlignment="Top">
<input:ValidationSummary />
<ComboBox x:Name="xTest" ItemsSource="{Binding MyList}"
SelectedItem="{Binding MyItem,
Mode=TwoWay,
ValidatesOnDataErrors=True,
ValidatesOnNotifyDataErrors=True,
ValidatesOnExceptions=True,
NotifyOnValidationError=True}" />
</StackPanel>
</Grid>
public MyForm()
{
InitializeComponent();
// you don't need anything here to have the validations work
}