My grid's item source is a datatable that has only one field ID (Primary key). I have bound this field with Datagrid template column but at runt开发者_JAVA百科ime when I pass duplicate or null value to this ID inside datagrid column, no exception is caught and datagridtextbox doesn't show any error. My code is given below;
<UserControl.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>
</UserControl.Resources>
<DataGrid AutoGenerateColumns="False" Name="UserDataGrid" VerticalAlignment="Top"
Width="381" ItemsSource="{Binding DataTableProperty, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnValidationError=True}"
IsSynchronizedWithCurrentItem="true" >
<DataGrid.Columns>
<DataGridTextColumn Header="UserName" EditingElementStyle=
"{StaticResource errorStyle}">
<DataGridTextColumn.Binding>
<Binding Path="ID" ValidatesOnExceptions="True" ValidatesOnDataErrors="True" NotifyOnValidationError="True" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" >
<Binding.ValidationRules>
<ExceptionValidationRule ValidationStep="UpdatedValue"></ExceptionValidationRule>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Can anybody please help me, what I am doing wrong ?
You probably need to validate that manually. Check WPF DataGrid Practical Examples: Validation with Bound DataSets.
精彩评论