Answer the question
In order to leave comments, you need to log in
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>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question