0

I am struggling with displaying a validation error message. I have set a validation condition, but still cannot display a validation message. I would like to have a little message above my TextBox when data is not valid.

My c# code is follwing:

 public class Child
{
    public string Name { get; set; }
    public int Age { get; set; }
}
public partial class MainWindow : Window
{
    List<Child> children = new List<Child>();
    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        children.Add(new Child { Name = "johny", Age = 22 });
        children.Add(new Child { Name = "johnsy", Age = 212 });
        children.Add(new Child { Name = "johdny", Age = 222 });
        pnlChildren.DataContext = children;
    }
}
public class AgeValidation : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        int age;
        if (Int32.TryParse(value.ToString(), out age))
        {
            if (age >= 3)
            {
                return new ValidationResult(true, null);

            }
        }
        return new ValidationResult
            (false, "Name cannot be more than 3 characters long.");
    }

}

and XAML:

<Window x:Class="WpfApplication9.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication9"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel Name="pnlChildren">
    <ListBox Height="100" Width="120" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name}"/>
                    <TextBlock Text=" ," />
                    <TextBlock Text="{Binding Path=Age}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <TextBlock Text="Name:" />
        <TextBox Text="{Binding Path=Name}" Grid.Column="1" />
        <TextBlock Text="Age:" Grid.Row="1" />
        <TextBox Grid.Row="1" Grid.Column="1" >
            <TextBox.Text>
                <Binding Path ="Age" UpdateSourceTrigger="LostFocus" >
                    <Binding.ValidationRules>

                        <local:AgeValidation />
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>
        </TextBox>

    </Grid>
</StackPanel>

hegendroffer
  • 123
  • 1
  • 13
  • I tried to follow, but it is still very hard to apply it into my code. Could you please give just some hint? – hegendroffer May 15 '16 at 20:04
  • set a custom `Validation.ErrorTemplate` with error message for `TextBox`. there are many examples in the Web – ASh May 15 '16 at 20:16

0 Answers0