2012-05-15 4 views
0

DataGrid의 행에 잘못된 입력을했을 때 유효성 검사 오류를 알리고 싶습니다.WPF MVVM의 IDataErrorInfo() 인터페이스

<DataGridTextColumn Header="Name" Binding="{Binding Path=Name, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"/> 
<DataGridTextColumn Header="Age" Binding="{Binding Path=Age}"/> 
<DataGridTextColumn Header="Date Of Birth" Binding="{Binding Path=DateOfBirth, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}"/> 

다음 속성을 사용하여 ViewModel에 IDataErrorInfo를 구현했습니다.

public string this[string propname] 
     { 
      get 
      { 
       switch (propname) 
       { 
        case "Age": 
         int age=0; 
         if (int.TryParse("Age", out age)) 
         { 
          if (age <= 0 && age > 99) 
          { 
           return "Please enter the valid Age..."; 
          } 
         } 
         else 
          return "Please enter the valid Age..."; 

         break; 
        case "Name": 
         if (string.IsNullOrEmpty("Name")) 
         { 
          return "Enter the valid Name"; 
         } 
         break; 
        case "Address": 
         if (string.IsNullOrEmpty("Address")) 
         { 
          return "Enter the valid Address"; 
         } 
         break; 
        case "DateOfBirth": 
         DateTime datetime; 
         if (!DateTime.TryParse("DateOfBirth", out datetime)) 
         { 
          return "Please enter the valid Date of Birth"; 
         } 
         break; 
       } 
       return string.Empty; 
      } 
     } 

그러나 유효성 검사는 수행되지 않습니다. DataGridCell의 DateTime 속성에 텍스트를 입력하면 유효성 오류를 나타내는 빨간색 풍선이 있어야한다는 요구 사항이 필요합니다.

누구나 가능합니까?

답변

3

이 줄 :

if (int.TryParse("Age", out age)) 

정확하지 않을 수 있습니다.

당신은 빨간 풍선 당신이 빨간 풍선 제공해야 표시하려면 다음은 빨간색하게

<DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <TextBlock Style="{StaticResource ResourceKey=TextBlockStyle}" 
        Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"/> 
    </DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 

내가 당신에게 맡겨 :

<Style x:Key="TextBlockStyle" TargetType="{x:Type TextBlock}"> 
    <Style.Triggers> 
     <Trigger Property="Validation.HasError" Value="true"> 
      <Setter Property="ToolTip" 
        Value="{Binding RelativeSource={RelativeSource Self}, 
        Path=(Validation.Errors)[0].ErrorContent}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

그리고있다.