スポンサーサイト

上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

IDataErrorInfoを使用したデータ検証

 前回は例外を利用したデータ検証でしたが、今回はIDataErrorInfoを使用したデータ検証の方法です。
 まずはViewModel(データバインディングするクラス)にIDataErrorInfoを実装します。
using Livet;
using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Validation2.ViewModels
{
    public class MainWindowViewModel : ViewModel, IDataErrorInfo
    {
        private Dictionary<string, string> Errors = new Dictionary<string, string>();


        #region Value1変更通知プロパティ
        private int _Value1;

        public int Value1
        {
            get
            { return _Value1; }
            set
            { 
                if (_Value1 == value)
                    return;
                _Value1 = value;
                
                // データ検証
                Errors["Value1"] = value < 0 ? "0以上の数値を入力してください。" : null;

                RaisePropertyChanged();
            }
        }
        #endregion

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

        public string this[string columnName]
        {
            get 
            {
                return Errors.ContainsKey(columnName) ? Errors[columnName] : null;
            }
        }
    }
}
 IDataErrorInfoのメンバーには、Errorプロパティとインデクサがありますがデータ検証に使用するのはインデクサの方だけです。そのためこのサンプルコードではErrorプロパティは実装しません。
 インデクサでは指定したプロパティのエラーメッセージを返す必要があります。
 エラーがない場合はnullを返します。
 このサンプルコードではプロパティのsetterでデータ検証を行いエラーメッセージをErrorsに格納し、インデクサでErrorsから指定されたプロパティのエラーメッセージを返しています。
 インデクサで各プロパティのデータ検証を行い、その結果を返すようにしてもOKです。
 どのような方法でもよいのでデータ検証の結果をインデクサで返すようにすれば大丈夫です。

 View側のコードは前回のコードとほぼ同じです。
<Window x:Class="Validation2.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
        xmlns:l="http://schemas.livet-mvvm.net/2011/wpf"
        xmlns:v="clr-namespace:Validation2.Views"
        xmlns:vm="clr-namespace:Validation2.ViewModels"
        Title="MainWindow" Height="350" Width="525">
    
    <Window.DataContext>
        <vm:MainWindowViewModel/>
    </Window.DataContext>
    
    <Window.Resources>
        <ControlTemplate x:Key="ErrorTemplate">
            <DockPanel>
                <TextBlock DockPanel.Dock="Right" Foreground="Red" Margin="5,0"
                        Text="{Binding ElementName=adornedElement, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>

                <Border BorderBrush="Red" BorderThickness="1"
                    Width="{Binding ElementName=adornedElement, Path=ActualWidth}"
                    Height="{Binding ElementName=adornedElement, Path=ActualHeight}">
                    <AdornedElementPlaceholder Name="adornedElement"/>
                </Border>
            </DockPanel>
        </ControlTemplate>
    </Window.Resources>    
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="50"/>
        </Grid.ColumnDefinitions>
        
        <TextBlock Grid.Column="0" Text="IDataErrorInfoを使用:"/>
        <TextBox Grid.Column="1"
                 Validation.ErrorTemplate="{StaticResource ErrorTemplate}"
                 Text="{Binding Value1, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
    </Grid>
</Window>
 エラーメッセージの表示の方法(エラーテンプレート)は前回と同じです。
 違う点は、バインディング時に「ValidatesOnDataErrors」をtrueにすることです。
 IDataErrorInfoを使用したデータ検証を行う場合は必ずValidatesOnDataErrorsをtrueにする必要があります。
 結果は以下の様になります。
データ検証2
スポンサーサイト

テーマ : プログラミング
ジャンル : コンピュータ

コメントの投稿

非公開コメント

カレンダー
05 | 2015/06 | 07
- 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 - - - -
最新記事
月別アーカイブ
全記事表示リンク

全ての記事を表示する

カテゴリ
タグ

C# WPF ClosedXML BlendSDK Path データ検証 Rx XML 

リンク
最新コメント
検索フォーム
RSSリンクの表示
Amazon
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。