3

複数のViewModelを使用するアプリケーションがあります。一部のプロパティには DataAnnotations があります。

 [Required(ErrorMessage = "Field 'Range' is required.")]
    [Range(1, 10, ErrorMessage = "Field 'Range' is out of range.")]
    public int Password
    {
        get
        {
            return _password;
        }
        set
        {
            if (_password != value)
            {
                _password = value;
                RaisePropertyChanged("Password");
            }
        }
    }

すべてのビューモデルに IDataErrorInfo または INotifyDataErrorInfo インターフェイスを実装することで、どのように検証を行うことができますか?

This articleを使用していますが、プロパティ変更時に検証し、必須フィールドを検証しません。

4

2 に答える 2

2

を使用した簡単な例を次に示しますIDataErrorInfo。これは、開始するのに役立ちます。

XAML:

<Grid>
    <Grid.Resources>
        <ControlTemplate x:Key="LeftErrorTemplate">
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding AdornedElement.(Validation.Errors).[0].ErrorContent, ElementName=ErrorAdorner}" Background="Red" Foreground="White" FontWeight="Bold" VerticalAlignment="Center"/>
                <AdornedElementPlaceholder x:Name="ErrorAdorner">
                    <Border BorderBrush="Red" BorderThickness="1" />
                </AdornedElementPlaceholder>
            </StackPanel>
        </ControlTemplate>
    </Grid.Resources>
    <TextBlock Height="23" HorizontalAlignment="Left" Margin="158,66,0,0" Name="textBlock1" Text="Name" VerticalAlignment="Top" Width="44" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="217,65,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" 
             Text="{Binding Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" 
             Validation.ErrorTemplate="{StaticResource LeftErrorTemplate}"/>
</Grid>

コードビハインド:

using System;
using System.Windows;
using System.ComponentModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            var vm = new ViewModel();

            this.DataContext = vm;
        }
    }

    public class ViewModel : ObservableBase, IDataErrorInfo
    {
        private string _Name;

        public string Name
        {
            get { return _Name; }
            set
            {
                _Name = value;
                OnPropertyChanged("Name");
            }
        }

        public string Error
        {
            get { throw new NotImplementedException(); }
        }

        public string this[string columnName]
        {
            get
            {
                string errorMessage = string.Empty;

                switch (columnName)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(Name))
                            errorMessage = "Enter name";
                        else if (Name.Trim() == string.Empty)
                            errorMessage = "Enter valid name";
                        break;
                }
                return errorMessage;
            }
        }

    }

    public class ObservableBase : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
于 2013-08-04T09:46:15.743 に答える