WPF - MVVM:Datagrid验证,Tooltip wrih错误消息不起作用

时间:2013-10-13 14:16:08

标签: c# wpf validation mvvm datagrid

我正在通过验证规则对datagrid单元格进行验证。

我对单元格编辑进行了验证,但没有办法显示错误消息的工具提示。当单元格值超出范围时,我得到错误图标,但不是错误消息工具提示。

这是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 个答案:

答案 0 :(得分: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>

尝试一下,让我们知道它是否有效。 :)