tags:

views:

249

answers:

1

I am having problems displaying the Validation.Errors in my ItemsControl. The Validation.Errors does not contain anything. I am NOT using BindingGroup but I use my own custom TextBoxes. Here is the ItemsControl code:

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

Can anyone explain why the Validation.Errors contains nothing when I bind to the ItemsControl?

A: 

I did my validation very similarly:

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

The only drawback is that it does not fire the validation until you enter the first character. You can force it do it in the constructor like this:

System.Windows.Data.BindingExpression be;
DependencyProperty txtProp = System.Windows.Controls.TextBox.TextProperty;
be = PatientFirstName.GetBindingExpression(txtProp);
be.UpdateSource();
Jay
Thanks for the reply but I think you misunderstood the question! I have created a Custom TextBox which fires validation even if you don't put anything in the TextBox. But the Validation.Errors property which is bind to ItemsControl returns null even when the Window is invalid.
azamsharp
What are you using to create the error you're looking for?In my code behind class I have a validator class called "NameRequiredRule". It adds an error object if there's a validation error.
Jay