How to get rid of the red border when the DataGrid cell is invalid?

How can I do it? I want to get rid of this annoying red border that appears in every invalid datagrid cell.

+6
source share
5 answers

Set ValidateOnDataErrors and ValidatesOnExpcetions to False to bind your cell. In case you want to receive confirmation, you must issue a verification template for your control. Please refer to my answer here - WPF validation error style similar to Silverlight

+2
source

You can simply add this row to your DataGrid:

<DataGrid Validation.ErrorTemplate="{x:Null}" /> 
+6
source

I hated the red border because it was placed in the adorner and the adorners were sitting on top of the window, which means that if your element is partially / completely hidden by another element (for example, it is in the grid), then the full adorner still shows: (

This did not mean that I did not need any adjustment, so I can still select my elements in pink or change their foreground and correctly clear the DataGridCell. You can do this using a style trigger.

Hope this helps!

 <DataGrid.Resources> <Style TargetType="{x:Type TextBlock}" x:Key="TextBlockErrorStyle"> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <!-- Just the adorned element means NO RED BORDER --> <AdornedElementPlaceholder Name="controlWithError" /> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="Foreground" Value="Red"/> <Setter Property="Background" Value="Pink"/> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/> </Trigger> </Style.Triggers> </Style> </DataGrid.Resources> 

...

 <DataGridTemplateColumn Header="Description" MinWidth="150"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Path=Description, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" Style="{StaticResource ResourceKey=TextBlockErrorStyle}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> 
+1
source

I had the same problem, but in my case I did not use IDataError or INotifyDataErrorInfo . I use custom ValidationRule and styles to handle my validation and presentation logic. I already used a DataGrid RowStyle to display a custom style for an error row, so I thought it would be easy to do something like this with a DataGridCell.

Notes:

  • You cannot just define a style and set DataGrid.CellStyle . Instead, you should use ElementStyle and / or EditingElementStyle .

  • The TargetType style must match the type of DataGridColumn that the cell uses. So for a DataGridComboBoxColumn TargetType must be a ComboBox

DataGrid XAML Column:

 <DataGridComboBoxColumn Header="Column1" ItemsSource="{Binding Path=Column1Path}" DisplayMemberPath="Column1DisplayPath" ElementStyle="{StaticResource DGComboColValidationStyle}" EditingElementStyle="{StaticResource DGComboColValidationStyle}"> <DataGridComboBoxColumn.SelectedItemBinding> <Binding Path="Column1Path" UpdateSourceTrigger="LostFocus"> <Binding.ValidationRules> <Validation:CustomValidationRule ValidationStep="UpdatedValue" ValidatesOnTargetUpdated="True" /> </Binding.ValidationRules> </Binding> </DataGridComboBoxColumn.SelectedItemBinding> </DataGridComboBoxColumn> 

Style definitions

 <Style x:Key="DGComboColValidationStyle" TargetType="{x:Type ComboBox}"> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <Grid> <Border BorderThickness="1" BorderBrush="{Binding ElementName=adorner1, Path=DataContext[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}" CornerRadius="3"> </Border> <AdornedElementPlaceholder Name="adorner1"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="DGTextColValidationStyle" TargetType="{x:Type TextBlock}"> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <Grid> <Border BorderThickness="1" BorderBrush="{Binding ElementName=adorner2, Path=DataContext[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}" CornerRadius="3"> </Border> <AdornedElementPlaceholder Name="adorner2"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> <Style x:Key="DGRowValidationStyle" TargetType="{x:Type DataGridRow}"> <Setter Property="ValidationErrorTemplate"> <Setter.Value> <ControlTemplate x:Name="DGRowValidationTemplate"> <Grid> <Ellipse Width="12" Height="12" Stroke="Black" StrokeThickness="0.5" Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}"/> <TextBlock FontWeight="Bold" Padding="4,0,0,0" Margin="0" VerticalAlignment="Top" Foreground="White" Text="!" ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent.ValidationMessage}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> <Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="BorderThickness" Value="2"/> <Setter Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent.ValidationType, Converter={StaticResource ValidationTypeColorConverter}}"/> </Trigger> <Trigger Property="Validation.HasError" Value="false"> <Setter Property="BorderThickness" Value="0"/> <Setter Property="BorderBrush" Value="Transparent" /> </Trigger> </Style.Triggers> </Style> <Style x:Key="DGCellStyle" TargetType="{x:Type DataGridCell}"> <Style.Triggers> <Trigger Property="Validation.HasError" Value="false"> <Setter Property="BorderThickness" Value="0"/> <Setter Property="BorderBrush" Value="Transparent" /> <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridRow}}, Path=(Validation.Errors)[0].ErrorContent.ValidationMessage}"/> </Trigger> </Style.Triggers> </Style> 

Link: http://social.msdn.microsoft.com/Forums/vstudio/en-US/6d2d6513-7bca-4359-a12b-46da3c380b0a/wpf-4-datagrid-editingelementstyle-and-validationerrortemplate-adorner-layer?forum= wpf

+1
source
0
source

Source: https://habr.com/ru/post/898782/


All Articles