0

我有一个使用 DataGridTemplateColumns 构建的数据网格。当存在验证错误时,我们会将其显示在 CellTemplate 的 Textblock 上。发生滚动时,错误样式会在滚动离开页面后丢失。

在我的控制资源中:

<ControlTemplate x:Key="validationTemplate">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <TextBlock Foreground="Yellow" 
                       FontSize="20" 
                       VerticalAlignment="Center"
                       Margin="0,0,2,2">!</TextBlock>
            <Border Grid.Column="1" 
                    BorderBrush="Yellow" 
                    BorderThickness="1" 
                    Margin="0" 
                    Padding="0"
                    Height="19">
                <AdornedElementPlaceholder/>
            </Border>
        </Grid>
    </ControlTemplate>

<Style x:Key="TextBlockValidationStyle" TargetType="{x:Type TextBlock}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError" Value="true">
                <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self},
                Path=(Validation.Errors)[0].ErrorContent}"/>
            </Trigger>
        </Style.Triggers>
    </Style>

在 DataGrid 中:

  <DataGridTemplateColumn Header="Destination Column">
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <ComboBox IsEditable="True"
                                      IsTextSearchCaseSensitive="{Binding ElementName=caseSensitiveSearch, Path=IsChecked}"
                                      ItemsSource="{Binding AllSuggestedNames}"
                                      TextSearch.TextPath="SuggestedName"
                                      Text="{Binding ColumnMapping.DestinationColumnName, UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Style="{StaticResource TextBlockValidationStyle}"
                                       Text="{Binding ColumnMapping.DestinationColumnName, 
                                                ValidatesOnDataErrors=True, 
                                                NotifyOnValidationError=True, 
                                                UpdateSourceTrigger=PropertyChanged}"
                                       Validation.ErrorTemplate="{StaticResource validationTemplate}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
4

1 回答 1

3

解决了!用 AdornerDecorator 包裹每个 CellTemplate 的 DataTemplate 内部,将其修复!

                        <DataTemplate>
                        <AdornerDecorator>
                            <TextBlock Style="{StaticResource TextBlockValidationStyle}"
                                   Text="{Binding ColumnMapping.DestinationColumnName, 
                                            ValidatesOnDataErrors=True, 
                                            NotifyOnValidationError=True, 
                                            UpdateSourceTrigger=PropertyChanged}"
                                   Validation.ErrorTemplate="{StaticResource validationTemplate}"/>
                        </AdornerDecorator>
                    </DataTemplate>
于 2012-07-20T19:05:18.067 回答