Q
Q
Qulac2015-06-04 17:01:00
WPF
Qulac, 2015-06-04 17:01:00

How to do validation in DataGrid?

<Window x:Class="WpfApplicationTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Users}">
            <DataGrid.Resources>
                <Style x:Key="ErrorStyle" TargetType="{x:Type TextBox}">
                    <Setter Property="Padding" Value="-2" />
                    <Style.Triggers>
                        <Trigger Property="Validation.HasError" Value="True">
                            <Setter Property="Background" Value="Red" />
                                <Setter Property="ToolTip"
                                    Value="{Binding RelativeSource={RelativeSource Self},
            Path=(Validation.Errors)[0].ErrorContent}" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Header="Имя" Binding="{Binding Path=FirstName,ValidatesOnExceptions=True}"
                                    EditingElementStyle="{StaticResource ErrorStyle}" />
                <DataGridTextColumn Header="Фамилия" Binding="{Binding Path=LastName, ValidatesOnExceptions=True}" EditingElementStyle="{StaticResource ErrorStyle}"  />
                <DataGridTextColumn Header="Отчество" Binding="{Binding Path=MiddleName,ValidatesOnExceptions=True}" EditingElementStyle="{StaticResource ErrorStyle}"  />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

In this example, if there is an error in the data object, the cell should be highlighted with a red frame and a tooltip with an error message will appear. It does not work, I no longer know where to poke, as the small red "!" Icon showed, and it shows. Poke your nose, please, what's wrong here.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2015-06-04
@yarosroman

Do fields have validation attributes in the model?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question