【问题标题】:WPF Datagrid Cross row ValidationWPF Datagrid 跨行验证
【发布时间】:2010-12-10 21:15:06
【问题描述】:

有没有人有任何关于 WPF Datagrid 的跨行验证的示例。单元级验证和行级验证不符合我的要求。我试图尽可能地坚持使用 MVVM。我的最后一个选择是使用后面的代码。所以基本上我需要在网格中发生某些事情时访问 Itemssource。任何帮助深表感谢。 谢谢-雷伊

【问题讨论】:

  • 你在使用实体框架吗?
  • 是的,它是一个客户端服务器应用程序...

标签: wpf validation datagrid


【解决方案1】:

在后面的代码中为每个表添加一个部分类。

如果没有错误,属性 [HasNoError] 返回 true

属性 [Error] 以字符串形式返回错误

if(tablename.HasNoError)
{
// do your logic
}
else
{
// display tablename.Error
}

在xaml端使用绑定

<DataGridTextColumn  Binding="{Binding Path=ActualFieldName1, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged }" Header=" ActualFieldName1"  />

这是使用 IDataErrorInfo 的类示例-

public partial class tablename : IDataErrorInfo
{
    private Dictionary<string, string> errorCollection = new Dictionary<string, string>();
    public bool HasNoError
    {
        get
        {
            return string.IsNullOrWhiteSpace(Error);
        }
    }
    public string Error
    {
        get
        {
            if (errorCollection.Count == 0)
                return null;
            StringBuilder errorList = new StringBuilder();
            var errorMessages = errorCollection.Values.GetEnumerator();
            while (errorMessages.MoveNext())
                errorList.AppendLine(errorMessages.Current);
            return errorList.ToString();
        }
    }
    public string this[string fieldName]
    {
        get
        {
            string result = null;
            switch (fieldName)
            {
                case "ActualFieldName1":
                    if (string.IsNullOrWhiteSpace(this.ActualFieldName1))
                    {
                        result = "ActualFieldName1 is required.";
                    };
                     if (Other_Condition)
                    {
                        result = "Other Result";
                    };
                    break;
                case "ActualFieldName2":
                    if (string.IsNullOrWhiteSpace(this.ActualFieldName2))
                    {
                        result = "ActualFieldName2 is required.";
                    };
                    if (Other_Condition)
                    {
                        result = "Other Result";
                    };
                    break;
                    // and so
            }
            if (result != null && !errorCollection.ContainsKey(fieldName))
                errorCollection.Add(fieldName, result);
            if (result == null && errorCollection.ContainsKey(fieldName))
                errorCollection.Remove(fieldName);
            return result;
        }
    }
}

为了更好地添加一些样式以定位错误模板,请参阅示例

<Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <Border BorderBrush="Red" BorderThickness="1">
                        <Grid>
                            <AdornedElementPlaceholder x:Name="MyAdorner"/>
                            <Image Width="{Binding AdornedElement.ActualHeight, ElementName=MyAdorner}"  Margin="0" ToolTip="{Binding AdornedElement.(Validation.Errors)[0].ErrorContent, ElementName=MyAdorner}" HorizontalAlignment="Right"  VerticalAlignment="Center" Source="/Path/Exclamation.png" />
                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

【讨论】:

  • 我相信上述方法适用于单元级验证。对于我的多行验证,我发现了这篇文章,stackoverflow.com/questions/1729414/…,我还结合了你的单元格级别验证来显示无效单元格。抱歉,我无法将您的回复标记为答案,因为它没有为我的问题提供正确的解决方案。
猜你喜欢
  • 2012-12-09
  • 2018-09-06
  • 2011-09-29
  • 1970-01-01
  • 2011-06-29
  • 2011-02-22
  • 2011-05-05
  • 2015-10-15
  • 2011-07-03
相关资源
最近更新 更多