当DataGrid单元格无效时,如何摆脱红色边框?

时间:2011-10-06 16:53:56

标签: wpf

我该怎么做?我想摆脱每个无效数据网格单元上显示的恼人的红色边框。

5 个答案:

答案 0 :(得分:6)

您可以将此行添加到DataGrid:

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

答案 1 :(得分:2)

为您的单元格绑定设置ValidatesOnDataErrors和ValidatesOnExpcetions为False。 如果您需要验证,则必须为控件提供验证模板。请参考我的回答 - Validation Error Style in WPF, similar to Silverlight

答案 2 :(得分:2)

我遇到了同样的问题,但就我而言,我没有使用IDataErrorINotifyDataErrorInfo。我正在使用自定义ValidationRule和样式来处理我的验证逻辑和演示。我已经在使用DataGrid RowStyle来显示有错误的行的自定义样式,所以我认为使用DataGridCell做类似的事情很容易。

注意事项:

  1. 您不能只定义样式并设置DataGrid.CellStyle。相反,您必须使用ElementStyle和/或EditingElementStyle

  2. 样式TargetType必须与单元格正在使用的DataGridColumn类型匹配。因此,对于DataGridComboBoxColumn,TargetType应为ComboBox

  3. DataGrid列XAML:

    <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 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>
    

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

答案 3 :(得分:1)

我讨厌红色边框,因为它放在装饰品中,装饰品位于窗口顶部,这意味着如果您的元素被其他元素部分/完全隐藏(就像它在网格中),那么完整的装饰品仍会显示:(

这并不意味着我不想要一些自定义,所以我仍然可以用粉红色突出显示我的项目,或者更改它们的前景并让DataGridCell正确地剪掉一切。您可以使用样式触发器来完成此操作。

希望这有帮助!

<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>

答案 4 :(得分:0)