Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Datagrid row validation error not clearing after edit

Using a DataGrid in WPF, I am trying to get correct behaviour while using error validation via INotifyDataErrorInfo.

I have an ObservableCollection of a class that implements that interface, an bind that collection to the DataGrid. When there is an error, the cell will have a red border, and row will have a red ! in front. All default, all good. When still editing, when the error is gone, the red border and red ! will both disappear. So far, so good!

However, when I leave the row (via keyboard Enter/Tab or with the mouse), then come back it and then remove the error, the red cell border disappears, but the red ! stays.

I am aware this question has been raised before, for example here: WPF DataGrid validation errors not clearing. However, the solutions there do not resolve this, apart from hiding the row validation error altogether. (Which, in combination with something like the second answer here is also quite ok...)

Or is my problem rather that the user is able to leave the editing mode of the cell even though there is a validation error? Preferably, I would like to restrict this, and force the resolution of the error first, before further editing can occur, but I don't know how to enforce this without lots of code...

Here is the XML (the RowValidationErrorTemplate comes from here: link):

<UserControl x:Class="CustomDG"
             ...etc...
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             d:DataContext="{d:DesignInstance Type=viewmodels:TestViewModel}">
    <Grid>
        <DataGrid
            ItemsSource="{Binding Path=testCollection}" AutoGenerateColumns="False"
            RowHeight="18" CanUserResizeRows="False" RowHeaderWidth="18" >
            <DataGrid.RowValidationErrorTemplate>
              <ControlTemplate>
                <Grid Margin="0,-2,0,-2"
                    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.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name, 
                    ValidatesOnNotifyDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" >
                </DataGridTextColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>
like image 401
mennowo Avatar asked Jul 15 '15 09:07

mennowo


1 Answers

you need to raise notifyPropertyChanged with empty string after row committed or canceled edit to refresh the DataBinding From object and this will refresh your interface use it like that:

RaiseNotifyPropertyChanged(""); 
like image 83
Hisham Avatar answered Nov 05 '22 03:11

Hisham