2

c-sharp wpf デスクトップ アプリケーションでユーザー入力を検証する必要があります。データベースファーストのアプローチでエンティティフレームワークを使用しています。これは、Entity フレームワークによってデータベースから生成されたクラスです。

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

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

検証を実装するために、DataAnnotations でバディ クラスを使用しようとしています。

using System.ComponentModel.DataAnnotations;

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

これは私のxamlフォームです:

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

プロパティ「名前」の検証が機能しません。問題はどこだ?

4

1 に答える 1

0

この記事から:

WPF では、Silverlight とは異なり、データ注釈はコンポーネント (DataGrid) によって自動的に読み取られません。IDataErrorInfo インターフェイスを実装し、データ注釈を使用して手動で検証する必要があります。

于 2013-01-26T10:19:20.053 に答える