スポンサーサイト
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。
新しい記事を書く事で広告が消せます。
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にする必要があります。
結果は以下の様になります。