I need to validate some user input in a c-sharp wpf desktop application. I'm using Entity framework with database-first approach. This is the class generated from the database by the Entity framework:

namespace TestValidation
{
    using System;
    using System.Collections.Generic;

    public partial class person
    {
        public string Name { get; set; }
        public string Surname { get; set; }
    }
}

I'm trying to use a buddy class with DataAnnotations to implement validation:

using System.ComponentModel.DataAnnotations;

    namespace TestValidation
    {
        [MetadataType(typeof(person_validation))]
        public partial class person
        {
            public class person_validation
            {
                [Required]
                public string Name { get; set; }
            }
        }
    }

This is my xaml form:

<Window
        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:TestValidation" mc:Ignorable="d" x:Class="TestValidation.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded_1">
    <Window.Resources>
        <CollectionViewSource x:Key="personViewSource" d:DesignSource="{d:DesignInstance {x:Type local:person}, CreateList=True}"/>
    </Window.Resources>
    <Grid>
        <Grid x:Name="grid1" DataContext="{StaticResource personViewSource}" HorizontalAlignment="Left" Margin="129,95,0,0" VerticalAlignment="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
            <Label Content="Name:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
            <TextBox x:Name="nameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding Name, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
        </Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="162,217,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>

    </Grid>
</Window>

The validation of the property "Name" doesn't work. Where is the problem?

share|improve this question
    
Are you expecting the built-in validation to kick in? If so, it works only if you are using DbContext. There is no support for validation if you are using ObjectContext – Pawel Oct 31 '12 at 21:28

from this article:

in WPF, unlike in Silverlight, the data annotations aren’t automatically read by the components (DataGrid). You’ll have to implement the IDataErrorInfo interface and validate manually using the data annotations.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.