2013-10-13 59 views
0

我正在通過驗證規則實施對datagrid單元格的驗證。WPF - MVVM:Datagrid驗證,工具提示wrih錯誤消息不起作用

我得到了有關單元格編輯的驗證,但沒有辦法顯示帶有錯誤消息的工具提示。單元格值超出範圍時出現錯誤圖標,但不顯示錯誤消息工具提示。

這是XAML代碼:

<UserControl.Resources> 
    <local:DatagridViewModel x:Key="ViewModel"/> 
    <CollectionViewSource x:Key="cvsEntries" Source="{Binding LineItems, Source={StaticResource ViewModel}}"/> 

    <Style TargetType="{x:Type DataGridRow}"> 
     <Setter Property="ValidationErrorTemplate"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Image Source="../Images/error.png" ToolTip="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem}" Margin="0" Width="15" Height="15" /> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
</UserControl.Resources> 

<DataGrid x:Name="lineItemGrid" Grid.ColumnSpan="5" Grid.Row="13" Background="#FFD6E9F5" ItemsSource="{Binding Source={StaticResource cvsEntries}}" AutoGenerateColumns="False" CanUserDeleteRows="False" VerticalContentAlignment="Stretch" Margin="0,4,0,0" SelectionMode="Single" SelectedItem="{Binding SelectedLineItem}"> 
    <DataGrid.Resources> 
     <SolidColorBrush x:Key="DataGridDeployedTextForeground" Color="#FF409540"/> 
     <SolidColorBrush x:Key="DataGridObsoleteTextForeground" Color="#FF934440"/> 
     <SolidColorBrush x:Key="DataGridDraftTextForeground" Color="#FF654093"/> 

    </DataGrid.Resources> 
    <DataGrid.Columns> 

     <DataGridTextColumn Binding="{Binding Product, Mode=OneWay}" Header="Product" IsReadOnly="True"/> 
     <DataGridTextColumn Binding="{Binding State, Mode=OneWay}" Header="State" SortMemberPath="State" IsReadOnly="True"/> 
     <DataGridTextColumn Header="Cust.Opt"> 
      <DataGridTextColumn.Binding> 
       <Binding Path="CustomAttributeValue" UpdateSourceTrigger="PropertyChanged"> 
        <Binding.ValidationRules> 
          <local:PropertyValidation> 
          <local:PropertyValidation.ValidValue> 
           <local:ValueChecker 
            Minimum="{Binding Path=CustomAttributeMinValue, Source={StaticResource cvsEntries}}" 
            Maximum="{Binding Path=CustomAttributeMaxValue, Source={StaticResource cvsEntries}}"/> 
          </local:PropertyValidation.ValidValue> 
         </local:PropertyValidation>         
        </Binding.ValidationRules> 
       </Binding> 
      </DataGridTextColumn.Binding> 
     </DataGridTextColumn> 
    </DataGrid.Columns> 
</DataGrid> 

,這是驗證代碼:

public override ValidationResult Validate(object value, CultureInfo cultureInfo) 
{ 
    var result = new ValidationResult(true, null); 

    var minValue = System.Convert.ToInt32(ValidValue.Minimum); 
    var maxValue = System.Convert.ToInt32(ValidValue.Maximum); 
    var valueToCheck = minValue; 
    Error = "Error!!!"; 

    int.TryParse(value as string, out valueToCheck); 

    CanBeValidated = true; 
    if (minValue == -1 && maxValue == -1 && value == string.Empty) 
    { 
    value = null; 
    CanBeValidated = false; 
    } 

    if (CanBeValidated == true && (valueToCheck < minValue || valueToCheck > maxValue)) 
    { 
    result = new ValidationResult(false, this.Error); 
    } 
    else 
    { 
    result = new ValidationResult(true, "OK"); 
    } 

    return result; 
} 

}

和這個附加的屬性:

public class ValueChecker : DependencyObject 
{ 
    public static readonly DependencyProperty MinimumProperty = DependencyProperty.Register("Minimum", typeof(string), typeof(ValueChecker)); 

    public string Minimum 
    { 
     get 
     { 
     return (string)GetValue(MinimumProperty); 
     } 
     set 
     { 
     SetValue(MinimumProperty, value); 
     } 
    } 

    public static readonly DependencyProperty MaximumProperty = DependencyProperty.Register("Maximum", typeof(string), typeof(ValueChecker)); 

    public string Maximum 
    { 
     get 
     { 
     return (string)GetValue(MaximumProperty); 
     } 
     set 
     { 
     SetValue(MaximumProperty, value); 
     } 
    } 

由於任何線索在這個問題上。

+1

對於有效的結果,您應該返回'ValidationResult.ValidResult' – Ninglin

回答

0

獲得錯誤信息進行驗證的是這樣的:

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

試試吧,讓我們知道它的工作。 :)