开发者

WPF DataGrid - Validation Recommendations

开发者 https://www.devze.com 2022-12-30 23:06 出处:网络
We are implementing a WPF business application using MVVM.Currently we are trying to determine the best way to show validation errors for DataGrids.Currently we are trying to do so like this:

We are implementing a WPF business application using MVVM. Currently we are trying to determine the best way to show validation errors for DataGrids. Currently we are trying to do so like this:

1) A View user control contains a datagrid bound to an EntityCollection of Entity Framework objects located in the associated ViewModel

2) The ViewModel implments the IDataErrorInfo interface. In the ViewModel there are the normal properties including a property for implementing IDataErrorInfo.Item

3) The property which implements IDataErrorInfo.Item calls the validation from the underlying Model to determine if there are any validation issues

This works fine for any textbox or combobox on the view - validatiosn are being processed as expected. But with the datagrid we are stumped - no matter what we have tried the validation will not happen. Currently it is defined like this:

<D开发者_如何学JAVAataGrid x:Name="dgWikiNames" ItemsSource="{Binding Wiki_Names, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnSourceUpdated=True}" AutoGenerateColumns="False">
                        <DataGrid.RowValidationRules>
                            <DataErrorValidationRule ValidationStep="UpdatedValue"/>                            
                        </DataGrid.RowValidationRules>
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="First Name">
                                <DataGridTextColumn.Binding>
                                    <Binding Path="First_Name" ValidatesOnDataErrors="True">
                                        <Binding.ValidationRules>
                                            <DataErrorValidationRule/>
                                            <!--<local:CellDataInfoValidationRule ValidationStep="UpdatedValue"/>-->
                                        </Binding.ValidationRules>
                                    </Binding>
                                </DataGridTextColumn.Binding>
                            </DataGridTextColumn>
                            <DataGridTextColumn  Header="Is Primary" Binding="{Binding Primary_Flag}"/>
                        </DataGrid.Columns>
                    </DataGrid>

If anyone has any pointers or recommendations we'd appreciate it. Thanks


Add the following to your binding:

ValidatesOnDataErrors="True" NotifyOnValidationError="True"
0

精彩评论

暂无评论...
验证码 换一张
取 消