即使行具有错误验证规则,也继续添加或编辑数据网格行 [英] Continue adding or editing datagrid row even if row has error validation rule

查看:57
本文介绍了即使行具有错误验证规则,也继续添加或编辑数据网格行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望datagrid继续添加或编辑DataGrid行,即使行有错误验证规则



这是样本以澄清想法

I want datagrid to continue Adding or Editing DataGrid row even if row has error validation rule

this is sample in order to clarify the idea

<DataGrid Name="Dgrid"  AutoGenerateColumns="False" ItemsSource="{Binding Request.Obligations}" CanUserAddRows="True">


            <DataGrid.Columns>

                <DataGridTextColumn Binding="{Binding Id}" />
                <DataGridTextColumn Binding="{Binding Name}"/>
        <DataGridTextColumn Binding="{Binding Age}"/>

           </DataGrid.Columns>


            <DataGrid.RowValidationRules>
                <validationRules:ٌRowValidationRule ValidatesOnTargetUpdated="True" ValidationStep="UpdatedValue"  />
            </DataGrid.RowValidationRules>

            <DataGrid.RowValidationErrorTemplate>
                <ControlTemplate>
                    <!--<Grid Margin="0,-2,0,-2"
            ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}},Path=(Validation.Errors)[0].ErrorContent}">-->
                    <Grid Margin="0,-2,0,-2">
                        <Grid.ToolTip>
                            <MultiBinding Converter="{StaticResource DataGridRowMultiConverter}">
                                <Binding Path="(Validation.Errors)[0].ErrorContent"
                                         RelativeSource="{RelativeSource Mode=FindAncestor,
                                                AncestorType=DataGridRow}" />
                                <Binding Path="DataContext"
                                         RelativeSource="{RelativeSource Mode=FindAncestor,
                                                AncestorType=Window}" />
                            </MultiBinding>
                        </Grid.ToolTip>
                        <Ellipse StrokeThickness="0" Fill="Red" Width="{TemplateBinding FontSize}"
                                 Height="{TemplateBinding FontSize}" />
                        <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White"
                                   HorizontalAlignment="Center" />
                    </Grid>
                </ControlTemplate>
            </DataGrid.RowValidationErrorTemplate>

        </DataGrid>



如果用户输入了包含无效数据的新行,他将无法添加新行新行将丢失,现有数据无法编辑。



简而言之,我想停止RowValidationRules的行为并使其jop告诉用户他已输入无效数据而没有阻止他。



搜索后我发现此代码:


If a user entered a new row with invalid data, he will not be able to add a new row and the new row will be missing and the existing data can not be edited.

In short, I want to stop the behavior of RowValidationRules and make its jop to tell the user that he has entered invalid data without stoping him.

After search i found this code:

protected override void OnCanExecuteBeginEdit(System.Windows.Input.CanExecuteRoutedEventArgs e)
        {
            var hasCellValidationError = false;
            var hasRowValidationError = false;
            const BindingFlags bindingFlags =
                BindingFlags.FlattenHierarchy | BindingFlags.NonPublic | BindingFlags.Instance;
            var cellError = this.GetType().BaseType.GetProperty("HasCellValidationError", bindingFlags);
            var rowError = this.GetType().BaseType.GetProperty("HasRowValidationError", bindingFlags);

            if (cellError != null)
                hasCellValidationError = (bool)cellError.GetValue(this, null);
            if (rowError != null)
                hasRowValidationError = (bool)rowError.GetValue(this, null);

            base.OnCanExecuteBeginEdit(e);
            if ((!e.CanExecute && hasCellValidationError) || (!e.CanExecute && hasRowValidationError))
            {
                e.CanExecute = true;
                e.Handled = true;
            }
        }



但我发现我可以编辑行但无法添加更多行



我尝试过:



继续添加或编辑DataGrid行,即使行有错误验证规则


but I found that I can edit rows but can't add more rows

What I have tried:

continue Adding or Editing DataGrid row even if row has error validation rule

推荐答案

Quote:

继续添加或编辑datagrid行,即使行有错误验证规则

Continue adding or editing datagrid row even if row has error validation rule



如果你不想要尊重它,为什么还需要验证规则呢?



最简单的解决方案是不是验证规则。


Why do you want a validation rules if you don't want to have to respect it ?

The easiest solution is not validation rule.


这篇关于即使行具有错误验证规则,也继续添加或编辑数据网格行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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