2009-06-30 75 views
0

我在显示我的ItemsControl中的Validation.Errors时出现问题。 Validation.Errors不包含任何内容。我不使用BindingGroup,但我使用我自己的自定义文本框。这里是ItemsControl的代码:WPF Validation.Errors Contains Nothing

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

任何人都可以解释为什么Validation.Errors包含没事的时候我绑定到ItemsControl的?

回答

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(); 
+0

谢谢你的答复,但我想你误解了这个问题!我创建了一个自定义文本框,即使您没有在TextBox中放置任何内容,也会触发验证。但是即使窗口无效,绑定到ItemsControl的Validation.Errors属性也会返回null。 – azamsharp 2009-06-30 19:05:39