为什么无法在validationSummary中显示验证错误?

发布于 2024-09-17 19:25:00 字数 1409 浏览 3 评论 0原文

我有一个表单,在实体元数据类中设置了一些验证。然后通过VM将实体实例绑定到UI。如下所示:

Xaml 类似:

   <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>

代码隐藏类似:

public MyForm()
{
  InitializeComponent();
  this.xTest.BindingValidationError +=new EventHandler<ValidationErrorEventArgs>((s,e)=>{

  BindingExpression be = this.xTest.GetBindingExpression(ComboBox.SelectedItemProperty);
  be.UpdateSource();

  if (e.Action == ValidationErrorEventAction.Added)
    ((ComboBox)s).Foreground = new SolidColorBrush(Colors.Red);
  });
}

元数据类似:

[Required]
 public string Name { get; set; }

 [RequiredAttribute]
 public int MyItemID { get; set; }

但是在运行应用程序时,我在 valudationSummary 中没有显示任何内容。 对于 CombBox,即使存在错误,看起来 BindingValidationError 事件也永远不会被触发。 如何解决呢?

I have a form with some validations set in entity metadata class. and then binding entity instance to UI by VM. Something as below:

Xaml like:

   <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>

Code-behind like:

public MyForm()
{
  InitializeComponent();
  this.xTest.BindingValidationError +=new EventHandler<ValidationErrorEventArgs>((s,e)=>{

  BindingExpression be = this.xTest.GetBindingExpression(ComboBox.SelectedItemProperty);
  be.UpdateSource();

  if (e.Action == ValidationErrorEventAction.Added)
    ((ComboBox)s).Foreground = new SolidColorBrush(Colors.Red);
  });
}

Metadata like:

[Required]
 public string Name { get; set; }

 [RequiredAttribute]
 public int MyItemID { get; set; }

But when running the app, I got nothing display in valudationSummary.
For CombBox, even there is error, looks like BindingValidationError event is never fired.
How to resolve it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2024-09-24 19:25:00

为什么要使用显式 UpdateSourceTrigger?

当绑定更新源对象时,Silverlight 验证发生在绑定框架内部。这样,就不会出现绑定验证错误,因为您永远不会告诉绑定更新源对象。嗯,实际上是这样,但它发生在验证错误事件处理程序内。您已经编写了先有鸡还是先有蛋的代码。

  • 删除绑定上的 UpdateSourceTrigger 或将其设置为 Default
  • 删除对 BindingExpression.UpdateSource 的显式调用。
  • 删除将 ComboBox 前景设置为红色 - 您使用的是 NotifyOnValidationError=True,这样就无需手动为控件着色。
  • 从绑定中删除 DisplayMemberPath

所以你的 XAML:

<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
}  

Why are you using an Explicit UpdateSourceTrigger?

Silverlight validation happens inside the binding framework, when the binding is updating the source object. The way you have this, there won't be a binding validation error because you never tell the binding to update the source object. Well, actually you do, but it happens inside the validation error event handler. You've written chicken-and-egg code.

  • Remove your UpdateSourceTrigger on your binding or set it to Default.
  • Remove the explicit call to BindingExpression.UpdateSource.
  • Remove setting the ComboBox foreground to red - you are using NotifyOnValidationError=True, which eliminates any need to manually color the control.
  • Remove the DisplayMemberPath from the binding

So your XAML:

<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> 

And your code:

public MyForm()   
{   
  InitializeComponent();   
  // you don't need anything here to have the validations work
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文