2

在我的 WPF 应用程序中,我有一个 ObservableCollection 项目。每个项目必须有一个唯一的名称,并且项目的名称必须以字母开头。我在实现 IDataErrorInfo 的基类中检查数据验证错误。问题是当用户输入现有名称时,椭圆和“!” 标志只出现在一行,而不是两行,但它们都有验证错误。这是我的 DataGrid 的一些代码。

    <DataGrid ItemsSource="{Binding Path=IconManagerModel.ConfigurationIcons, 
                             ValidatesOnDataErrors=True}" x:Name="IconsData">
        <DataGrid.Resources>
            <Style x:Key="errorStyle" TargetType="{x:Type TextBlock}" >
                <Setter Property="Padding" Value="2"/>
                <Style.Triggers>
    //Error style for names which not starts with letter
                    <Trigger Property="Validation.HasError" Value="True">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="ToolTip" Value="{Binding RelativeSource=
                                RelativeSource FindAncestor, 
                                AncestorType={x:Type DataGridRow}}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
    //Error style for duplicated names
                    <DataTrigger Binding="{Binding IsDuplicated}" Value="True">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="ToolTip" Value="Duplicated Name" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="ErrorEditStyle" 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 FindAncestor,
                                AncestorType={x:Type DataGridRow}}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>                
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
        <DataGrid.RowValidationErrorTemplate>
            <ControlTemplate>
//This template applies only for the row that has been edited. 
//Other row with the same IconId keeps default style
                <Grid ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type DataGridRow}}, 
                Path=(Validation.Errors)[0].ErrorContent}" >
                    <Ellipse StrokeThickness="0" Fill="Red"
                                        Width="{TemplateBinding FontSize}" 
                                        Height="{TemplateBinding FontSize}">
                    </Ellipse>
                    <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" 
                                        FontWeight="Bold" Foreground="White" 
                                        HorizontalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </DataGrid.RowValidationErrorTemplate>
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Icon Name">
            </DataGridTemplateColumn>
            <DataGridTextColumn  ElementStyle="{StaticResource ResourceKey=errorStyle}" 
                EditingElementStyle="{StaticResource ResourceKey=ErrorEditStyle}" 
                            Binding="{Binding IconId, ValidatesOnDataErrors=True,
                            NotifyOnValidationError=True,
                            UpdateSourceTrigger=PropertyChanged}"/>
        </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

提前致谢。

4

0 回答 0