Gridrow validation.errortemplate [英] Gridrow validation.errortemplate

查看:86
本文介绍了Gridrow validation.errortemplate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下gridview

I have the following gridview

                    <DataGrid x:Name="gridFactory" Style="{StaticResource DataGrid}" RowStyle="{StaticResource DataGridRow}" MouseMove="gridFactory_MouseMove" >
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Omschrijving" ElementStyle="{StaticResource CellErrorSytle}" Binding="{Binding Path=Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnNotifyDataErrors=True, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Width="2*" />
                        </DataGrid.Columns>
                    </DataGrid>

这是使用的模板:

        <!-- Datagridrow with error support -->
        <Style x:Key="DataGridRow"  TargetType="DataGridRow">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="ValidationErrorTemplate">
                        <Setter.Value>
                            <ControlTemplate>
                                <Image Source="/Mengblad;component/Images/Error.png" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},
                            Path=(Validation.Errors)[0].ErrorContent}" Margin="0" Width="11" Height="11" />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
        
        <!-- Datagridcell with error support -->
        <Style x:Key="CellErrorSytle" TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="Background" Value="Pink"/>
                    <Setter Property="Foreground" Value="Black"/>
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                </Trigger>
            </Style.Triggers>
        </Style>

gridview上有一个对象有一个  ValidationAttribute

The gridview is filled with a collection of objects with has a  ValidationAttribute

    public class IsUniquePlant :  ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            //get a reference to the object we are loading
            var obj = (Plant)validationContext.ObjectInstance;

            //check the database to make sure the item is unique
            var dbContent = new MengbladEntities();
            var dbPlant =
                (from p in dbContent.tblPlants
                 where p.ID != obj.DatabaseID && p.FabriekDesc == obj.Description
                 select new { p.ID }).FirstOrDefault();

            if(dbPlant == null)
            {
                return ValidationResult.Success;
            }
            else
            {
                return new ValidationResult($"Er bestaat al een plant met de naam {value.ToString()}");
            }
        }
    }

这是对象成员:

        private string description { get; set; }
        [IsUniquePlant]
        public string Description
        {
            get
            {
                return description;
            }
            set
            {
                description = value;
                ValidateModelProperty(value, "Description");
            }
        }

这种方法很完美,在rowheader中出现一个带有工具提示的错误符号和带有错误颜色为红色。

当我删除错误时,单元格颜色恢复正常,但是行头中的错误符号不会消失。



出了什么问题,为什么错误在单元格中消失但在行中没有????




提前谢谢任何帮助

Filip Top

This works perfect, in the rowheader comes an error symbol with a tooltip and the cell with the error colors red.
When I remove the error the cell colors back to normal but the error symbol in the rowheader doesn't dissapear.

What is going wrong and why is the error gone in the cell but not in the row???


Thanks in advance for any help
Filip Top

推荐答案

嗨    FilipTop,



DataGrid控件使您可以在单元格和行级别执行验证。通过单元级验证,可以在用户更新值时验证绑定数据对象的各个属性。使用行级验证,当用户提交对行的更改时,验证整个数据对象
。您还可以为验证错误提供自定义的视觉反馈,或使用DataGrid控件提供的默认视觉反馈。



请参阅以下具有实现行和单元格的示例使用DataGrid控件进行验证。



如何:使用DataGrid控件实现验证
$


Hi   FilipTop,

The DataGrid control enables you to perform validation at both the cell and row level. With cell-level validation, you validate individual properties of a bound data object when a user updates a value. With row-level validation, you validate entire data objects when a user commits changes to a row. You can also provide customized visual feedback for validation errors, or use the default visual feedback that the DataGrid control provides.

Please refer the following sample which have Implement row and cell Validation with the DataGrid Control.

How to: Implement Validation with the DataGrid Control

此外,如果您可以通过将有用的帖子标记为答案来关闭该主题,将不胜感激。如果您有新问题,可以开始新的主题 包含所有必要的代码段,以便其他任何人能够从头开始重现您的问题
以及有关结果的详细说明,包括任何异常消息。



最诚挚的问候,



Yong Lu

Besides, It would be appreciated if you could close the thread by marking helpful posts as an answer. If you have a new question you can start a new thread  with all necessary code snippets for anyone else to be able to reproduce your issue from scratch along with a detailed description about the results including any exception messages.

Best Regards,

Yong Lu


这篇关于Gridrow validation.errortemplate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆