WPF 数据网格无效指示器未清除有效输入

发布于 2024-10-18 02:07:06 字数 3307 浏览 1 评论 0原文

我有一个 DataGrid,它显示了一些东西。每个单元格都使用 IDataErrorInfo 接口进行验证。当用户将无效数据放入单元格时,验证会失败,并且单元格的背景会变成红色,并且行左侧会出现错误图标,从而警告用户。当用户更正数据时,单元格的背景恢复正常,但错误图标不会清除。即使我没有定义 RowValidationErrorTemplate,也会发生这种情况。

       <DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Top" Width="Auto" 
                  ItemsSource="{Binding Path=Records}" ColumnWidth="Auto" CanUserReorderColumns="False" 
                  CanUserResizeColumns="False" SelectionUnit="Cell" CanUserAddRows="True">

        <DataGrid.RowValidationRules>
            <vm:TimeoutRecordRowValidation ValidationStep="UpdatedValue"/>
        </DataGrid.RowValidationRules>

        <DataGrid.RowValidationErrorTemplate>
            <ControlTemplate>
                <Grid ToolTip="{Binding RelativeSource={RelativeSource
                    FindAncestor, AncestorType={x:Type DataGridRow}},
                    Path=(Validation.Errors)[0].ErrorContent}">
                    <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.Resources>
            <Style x:Key="DefaultColumnHeaderStyle" TargetType="DataGridColumnHeader">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Column.(ToolTipService.ToolTip)}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>

        <DataGrid.Columns>
            <DataGridTextColumn Header="LabelFamilyID&#x0a;XXX"
                                    Binding="{Binding Path=LabelFamilyID, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                                    ElementStyle="{StaticResource ErrorStyle}">
            </DataGridTextColumn>
            <DataGridTextColumn Header="ModelCapacityString&#x0a;WD????????"
                                    Binding="{Binding Path=ModelCapacityString, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                                    ElementStyle="{StaticResource ErrorStyle}">
            </DataGridTextColumn>
            <DataGridTextColumn Header="Timeout Value&#x0a;99:99:99"
                                    Binding="{Binding Path=TimeoutValue, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                                    ElementStyle="{StaticResource ErrorStyle}">
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

为了简洁起见,我删除了一些不相关的 XAML,例如 HeaderStyle 定义。

I have a DataGrid, which is showing some stuff. Each of the cells has a validation on it, using the IDataErrorInfo interface. When the user puts invalid data into the cell, the validation fails and the user is alerted, both by the cell's background turning red, and the error icon appearing to the left of the row. When the user corrects the data, the cell's background goes back to normal, but the error icon doesn't clear. This happens even if I don't define the RowValidationErrorTemplate.

       <DataGrid AutoGenerateColumns="False" Height="Auto" HorizontalAlignment="Stretch" Name="dataGrid1" VerticalAlignment="Top" Width="Auto" 
                  ItemsSource="{Binding Path=Records}" ColumnWidth="Auto" CanUserReorderColumns="False" 
                  CanUserResizeColumns="False" SelectionUnit="Cell" CanUserAddRows="True">

        <DataGrid.RowValidationRules>
            <vm:TimeoutRecordRowValidation ValidationStep="UpdatedValue"/>
        </DataGrid.RowValidationRules>

        <DataGrid.RowValidationErrorTemplate>
            <ControlTemplate>
                <Grid ToolTip="{Binding RelativeSource={RelativeSource
                    FindAncestor, AncestorType={x:Type DataGridRow}},
                    Path=(Validation.Errors)[0].ErrorContent}">
                    <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.Resources>
            <Style x:Key="DefaultColumnHeaderStyle" TargetType="DataGridColumnHeader">
                <Style.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Column.(ToolTipService.ToolTip)}" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>

        <DataGrid.Columns>
            <DataGridTextColumn Header="LabelFamilyID
XXX"
                                    Binding="{Binding Path=LabelFamilyID, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                                    ElementStyle="{StaticResource ErrorStyle}">
            </DataGridTextColumn>
            <DataGridTextColumn Header="ModelCapacityString
WD????????"
                                    Binding="{Binding Path=ModelCapacityString, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                                    ElementStyle="{StaticResource ErrorStyle}">
            </DataGridTextColumn>
            <DataGridTextColumn Header="Timeout Value
99:99:99"
                                    Binding="{Binding Path=TimeoutValue, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
                                    ElementStyle="{StaticResource ErrorStyle}">
            </DataGridTextColumn>
        </DataGrid.Columns>
    </DataGrid>

I have trimmed some of the XAML that is unrelated, like the HeaderStyle definitions, for brevity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨落星ぅ辰 2024-10-25 02:07:06

我直接在 DataGrid 下添加了以下内容。它删除了感叹号。这不是“修复”,但也不会产生意想不到的结果

<DataGrid.RowValidationErrorTemplate>
    <ControlTemplate>
        <Grid Margin="0,-12,0,-12" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
        </Grid>
    </ControlTemplate>
</DataGrid.RowValidationErrorTemplate>

I added the following directly under DataGrid. It removes the exclamation mark. It is not a "fix" but it does not have unexpected results either

<DataGrid.RowValidationErrorTemplate>
    <ControlTemplate>
        <Grid Margin="0,-12,0,-12" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent}">
        </Grid>
    </ControlTemplate>
</DataGrid.RowValidationErrorTemplate>
2024-10-25 02:07:06

我不确定它是什么,但在每个 DataGridTextColumns 中,

Mode=TwoWay

从每个 Binding 元素中删除 就可以了。

I'm not sure what it was, but in each of the DataGridTextColumns, removing the

Mode=TwoWay

from each of the Binding elements did the trick.

泪是无色的血 2024-10-25 02:07:06

这也删除了感叹号:

<Setter Property="ValidationErrorTemplate" Value="{x:Null}"/>

我希望 MS 在 VS2013 update 5 的最终版本中纠正它

this remove the exclamation mark too:

<Setter Property="ValidationErrorTemplate" Value="{x:Null}"/>

I hope MS correct it in the final version of VS2013 update 5

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文