Q065. 簡単に検証インジケーターを表示するには?
GrapeCity 社の InputMan for WPF に含まれている GcValidationIndicator を使うと簡単です。検証を行う要素名を ElementName プロパティに設定するだけです。
<im:GcValidationIndicator ElementName="GcNumber1"/>
以下、某所に上げたサンプルです。二つの数値コントロールの値を乗算した結果が 12 を超えると検証インジケーターを表示します。
まず ViewModel のコード、MVVM インフラは Livet を使ってます。IDataErrorInfo の実装が面倒に思うかも知れませんが、スニペットに登録しとけば簡単に実装できます。(ちなみに IDataErrorInfo のスニペットは Livet にはない)
Option Explicit On Option Strict On Imports Livet Imports System.ComponentModel Public Class MainWindowViewModel Inherits ViewModel Implements IDataErrorInfo #Region "Column変更通知プロパティ" Private _Column As Integer Public Property Column() As Integer Get Return _Column End Get Set(ByVal value As Integer) _errors("Column") = Nothing If (_Column <> value) Then _Column = value If Not Me.Validate Then _errors("Column") = "オーバーしてます。" Else Me.Row = Me.Row End If End If RaisePropertyChanged("Column") End Set End Property #End Region #Region "Row変更通知プロパティ" Private _Row As Integer Public Property Row() As Integer Get Return _Row End Get Set(ByVal value As Integer) _errors("Row") = Nothing If (_Row <> value) Then _Row = value If Not Me.Validate Then _errors("Row") = "オーバーしてます。" Else Me.Column = Me.Column End If End If RaisePropertyChanged("Row") End Set End Property #End Region #Region "メソッド" Private Function Validate() As Boolean ' 行列の乗算結果が 12 以下なら True Return (Me.Column * Me.Row) <= 12 End Function #End Region #Region "IDataErrorInfo の実装" Private ReadOnly _errors As New Dictionary(Of String, String) Public ReadOnly Property [Error] As String Implements IDataErrorInfo.Error Get Dim ret = String.Empty For Each e In _errors If (Not String.IsNullOrEmpty(e.Value)) Then ret += e.Value + Environment.NewLine End If Next Return ret End Get End Property Default Public ReadOnly Property Item(propertyName As String) As String Implements IDataErrorInfo.Item Get If (_errors.ContainsKey(propertyName)) Then Return _errors(propertyName) Else Return Nothing End If End Get End Property #End Region End Class
お次は View。GcValidationIndicator の ElementName プロパティに GcNumber の名前を指定するだけで、入力時のエラーを表示します。
<Window x:Class="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:local="clr-namespace:LivetWPFApplication1" xmlns:im="http://schemas.grapecity.com/windows/2010/inputman" Title="MainWindow" Height="130" Width="280" > <Window.DataContext> <local:MainWindowViewModel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition Height="Auto" /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition Width="80"/> <ColumnDefinition Width="30" /> <ColumnDefinition Width="80"/> <ColumnDefinition Width="30" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <im:GcNumber Grid.Row="1" Grid.Column="1" Name="GcNumber1" Value="{Binding Column, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True}" /> <im:GcNumber Grid.Row="1" Grid.Column="3" Name="GcNumber2" Value="{Binding Row,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, ValidatesOnDataErrors=True}" /> <im:GcValidationIndicator Grid.Row="1" Grid.Column="2" Height="16" Width="16" ElementName="GcNumber1" /> <im:GcValidationIndicator Grid.Row="1" Grid.Column="4" Height="16" Width="16" ElementName="GcNumber2" /> </Grid> </Window>
WPF の有償コントロールって結構値の張るものが多いですが、作業工数を考慮するとこれらのコントロールを使った方が大幅に工数を削減できます。サードパーティー製のコントロールは便利なものや面白いものが多いので、使えるものは有効に使った方が少しは幸せになれると思います。