我在ItemsControl中显示Validation.Errors时遇到问题。 Validation.Errors不包含任何内容。我不使用BindingGroup,但我使用自己的自定义TextBoxes。这是ItemsControl代码:
<ItemsControl x:Name="errorList" ItemsSource="{Binding Path = (Validation.Errors), ElementName=gvAddCustomer}" >
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="18" Text="{Binding Path=ErrorContent}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
My TextBox uses the ErrorTemplate to display the errors beside the TextBox control and it displays correctly with the error message. Here is the style:
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<TextBlock DockPanel.Dock="Right"
Foreground="Orange"
FontSize="12pt"
Text="{Binding ElementName=MyAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
</TextBlock>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder Name="MyAdorner" />
</Border>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
有人解释为什么在绑定到ItemsControl时Validation.Errors什么都不包含?
答案 0 :(得分:0)
我的验证非常相似:
<TextBox
Style="{StaticResource TextBoxValidationError}"
Name="PatientFirstName" TabIndex="0">
<TextBox.Text>
<Binding Path="Patient.PatientFirstName" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<bs:NameRequiredRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
唯一的缺点是,在您输入第一个字符之前,它不会触发验证。您可以强制它在构造函数中执行此操作:
System.Windows.Data.BindingExpression be;
DependencyProperty txtProp = System.Windows.Controls.TextBox.TextProperty;
be = PatientFirstName.GetBindingExpression(txtProp);
be.UpdateSource();