1

私のソリューションには、WPF プロジェクトと、database.i を使用AutoMapperしてオブジェクトをマップする WCF プロジェクトがあります。

私の質問は、ViewModel クラスで DataAnnotations を使用して、それらを WCF サービス プロジェクトから受け取ったオブジェクトにマップすることはできますか? Asp.net MVC プロジェクトで行うように

検証に DataAnnotations を使用したい

そのように

 public class MContact
 {
      [Required(ErrorMessage = " Name is required.")]
      [StringLength(50, ErrorMessage = "No more than 50 characters")]
      [Display(Name = "Name")]
      public string Name { get; set; }


      [Required(ErrorMessage = "Email is required.")]
      [StringLength(50, ErrorMessage = "No more than 50 characters")]
      [RegularExpression(".+\\@.+\\..+", ErrorMessage = "Valid email required e.g. abc@xyz.com")]
      public string Email { get; set; }


      [Display(Name = "Phone Number")]
      [Required]
      [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$",
            ErrorMessage = "Entered phone format is not valid.")]
      public string PhoneNumber { get; set; }

      public string Address { get; set; }
      [RegularExpression(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$",
    ErrorMessage = "Entered phone format is not valid.")]
      public string Mobil { get; set; }

 }

これは私のxamlコードです

<Window x:Class="WPFClient.AddNewContact"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="AddNewContact" Height="342" Width="432" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:WPFClient.PhoneBookServiceReference" Loaded="Window_Loaded">
<Window.Resources>
</Window.Resources>
<Grid>
    <GroupBox Header="Add Contact">
        <Grid HorizontalAlignment="Left" Margin="21,21,0,0" Name="grid1" VerticalAlignment="Top" Height="168" Width="357">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="194" />
                <ColumnDefinition Width="64*" />
            </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
                <RowDefinition Height="8*" />
            </Grid.RowDefinitions>

            <Label Content="Name:" Grid.Column="0" Grid.Row="0" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="0" Height="23" HorizontalAlignment="Left" Margin="0,3,0,6" Name="nameTextBox" VerticalAlignment="Center" Width="120" />

            <Label Content="Email:" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="0,3,0,6" Name="emailTextBox" VerticalAlignment="Center" Width="120" />

            <Label Content="Phone Number:" Grid.Column="0" Grid.Row="2" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="0,3,0,6" Name="phoneNumberTextBox" VerticalAlignment="Center" Width="120" />

            <Label Content="Mobil:" Grid.Column="0" Grid.Row="3" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox Grid.Column="1" Grid.Row="3" Height="23" HorizontalAlignment="Left" Margin="0,3,0,6" Name="mobilTextBox" VerticalAlignment="Center" Width="120" />


            <Label Content="Address:" Grid.Column="0" Grid.Row="4" HorizontalAlignment="Left" Margin="3" VerticalAlignment="Center" />
            <TextBox  Grid.Row="4" Grid.Column="1" Height="23" HorizontalAlignment="Left" Margin="0,3,0,6" Name="addressTextBox"   VerticalAlignment="Center" Width="151" />

        </Grid>
    </GroupBox>
         <Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="24,217,0,0" Name="btnAdd" VerticalAlignment="Top" Width="75" />
    <Button Content="Cancel" Height="23" HorizontalAlignment="Left" Margin="123,217,0,0" Name="btnCancel" VerticalAlignment="Top" Width="75" />

</Grid>

4

1 に答える 1

0

いいえ、IDataErrorInfo インターフェイスを手動で実装する必要があります。

shared-validation- code- between-a-wpf-client-a-wcf-service-and-entity-framework-4-1-using-data-annotations から:

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

于 2013-01-26T10:30:48.887 に答える