DataGridTemplateColumn.CellTemplate中的TextBlock在滚动时丢失验证样式

时间:2012-07-20 16:36:03

标签: wpf validation templates datagrid

我有一个使用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>

1 个答案:

答案 0 :(得分: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>