简体   繁体   English

WPF数据网格样式错误IDataErrorInfo

[英]WPF datagrid styling errors IDataErrorInfo

I'm using MVVM and have a datagrid with an editable column that I'm doing validation on: 我正在使用MVVM并且有一个带有可编辑列的数据网格,我正在对其进行验证:

<DataGridTemplateColumn Header="Key" Width="80">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Key}" />
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <vw:NumericTextBox Text="{Binding Key, Mode=TwoWay,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>

I've put a style in to show the error as a tooltip: 我已经设置了一个样式来将错误显示为工具提示:

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

So the validation is fired and the cell is outlined in red and the error message tooltip is displayed. 因此,将触发验证,并以红色标出单元格,并显示错误消息工具提示。

I have 2 problems, firstly when the user click out of the cell the cell remains outlined in red but the tooltip isn't displayed when hovered over. 我有两个问题,首先当用户单击单元格时,单元格仍以红色勾勒出轮廓,但悬停时不显示工具提示。 How do I get this to work? 我如何让它工作? The second problem is that there is an orange exclamation next to the row which I don't want. 第二个问题是该行旁边有一个我不想要的橙色感叹号。 I assume this is because of some default styling on the grid or the row. 我假设这是因为网格或行上的一些默认样式。 How do I get rid of it (the red outline is fine)? 我怎么摆脱它(红色轮廓很好)?

To remove the exclamation point you've got to edit the datagrid rowsytyle like this: 要删除感叹号,您需要编辑datagrid rowsytyle,如下所示:

<DataGrid ...> 
<DataGrid.RowStyle> 
    <Style TargetType="DataGridRow"> 
        <Setter Property="ValidationErrorTemplate" Value="{x:Null}"/> 
    </Style> 
</DataGrid.RowStyle> 
<!-- ... --> 
</DataGrid> 

For the first part - to show error on hover, you need to set tool tip property for TextBlock target type as well. 对于第一部分 - 要在悬停时显示错误,您还需要为TextBlock目标类型设置工具提示属性。 I use below two styles to show error from IDataErrorInfo on datagrid cell edit and also for hover. 我使用以下两种样式来显示IDataErrorInfo对datagrid单元格编辑的错误以及悬停。

<Style x:Key="EditCellStyleError" TargetType="{x:Type TextBox}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="CellStyleError" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

set these styles for your datagrid 为您的数据网格设置这些样式

<DataGridTextColumn .....
EditingElementStyle="{StaticResource EditCellStyleError}" ElementStyle="{StaticResource CellStyleError}"
</DataGridTextColumn>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM