验证工具提示在数据模板组合框 + Catel 上出现两次
Validation tool tip appears twice on datatemplate combobox + Catel
知道为什么当我将鼠标悬停在编辑模式的数据网格内的组合框中时,我在该控件上收到 2 "tool tip" 条验证消息。在验证摘要中它只出现一次。在 DatagridTextColumn 上,验证 "tool tip" 仅在编辑模式下出现一次。
<DataGrid CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False" ItemsSource="{Binding Detalle_OC, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
AutoGenerateColumns="False" Height="200" SelectionMode="Single" Name="Detalle_OC" EnableRowVirtualization="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Producto" Width="400">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Producto}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=catel:DataWindow}, Path=DataContext.Productos}"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=catel:DataWindow}, Path=DataContext.ProductoText, Mode=OneWayToSource}"
SelectedValue="{Binding Id_Producto, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" SelectedValuePath="Id_Producto" DisplayMemberPath="Producto"
IsTextSearchEnabled="False" StaysOpenOnEdit="True" HorizontalAlignment="Left" Width="350" IsEditable="True" behaviors:ComboBoxBehavior.CharacterCasing="Upper">
<ComboBox.Triggers>
<EventTrigger RoutedEvent="TextBoxBase.TextChanged">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
<DiscreteBooleanKeyFrame Value="True"
KeyTime="0:0:0" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ComboBox.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Cantidad" Width="100" Binding="{Binding Cantidad, ValidatesOnDataErrors=True, NotifyOnValidationError=True, TargetNullValue='' }"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
这是模型验证器。
public class CDetalle_OCValidator:ValidatorBase<cDetalle_OC>
{
protected override void ValidateFields(cDetalle_OC instance, List<IFieldValidationResult> validationResults)
{
if (!instance.Id_Producto.HasValue || instance.Id_Producto<=0)
{
validationResults.Add(FieldValidationResult.CreateError(cDetalle_OC.Id_ProductoProperty, "El producto es requerido"));
}
if (!instance.Cantidad.HasValue || instance.Cantidad.Value<=0)
{
validationResults.Add(FieldValidationResult.CreateError(cDetalle_OC.CantidadProperty, "La cantidad tiene que ser mayor a 0"));
}
}
protected override void ValidateBusinessRules(cDetalle_OC instance, List<IBusinessRuleValidationResult> validationResults)
{
// No business rules validations yet
}
}
问题的原因是同时使用了 ValidatesOnDataErrors 和 NotifyOnValidationError。一次只能使用一个。
知道为什么当我将鼠标悬停在编辑模式的数据网格内的组合框中时,我在该控件上收到 2 "tool tip" 条验证消息。在验证摘要中它只出现一次。在 DatagridTextColumn 上,验证 "tool tip" 仅在编辑模式下出现一次。
<DataGrid CanUserAddRows="True" CanUserDeleteRows="True" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserResizeColumns="False" ItemsSource="{Binding Detalle_OC, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"
AutoGenerateColumns="False" Height="200" SelectionMode="Single" Name="Detalle_OC" EnableRowVirtualization="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Producto" Width="400">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Producto}"></TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=catel:DataWindow}, Path=DataContext.Productos}"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=catel:DataWindow}, Path=DataContext.ProductoText, Mode=OneWayToSource}"
SelectedValue="{Binding Id_Producto, ValidatesOnDataErrors=True, NotifyOnValidationError=True}" SelectedValuePath="Id_Producto" DisplayMemberPath="Producto"
IsTextSearchEnabled="False" StaysOpenOnEdit="True" HorizontalAlignment="Left" Width="350" IsEditable="True" behaviors:ComboBoxBehavior.CharacterCasing="Upper">
<ComboBox.Triggers>
<EventTrigger RoutedEvent="TextBoxBase.TextChanged">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="IsDropDownOpen">
<DiscreteBooleanKeyFrame Value="True"
KeyTime="0:0:0" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ComboBox.Triggers>
</ComboBox>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Cantidad" Width="100" Binding="{Binding Cantidad, ValidatesOnDataErrors=True, NotifyOnValidationError=True, TargetNullValue='' }"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
这是模型验证器。
public class CDetalle_OCValidator:ValidatorBase<cDetalle_OC>
{
protected override void ValidateFields(cDetalle_OC instance, List<IFieldValidationResult> validationResults)
{
if (!instance.Id_Producto.HasValue || instance.Id_Producto<=0)
{
validationResults.Add(FieldValidationResult.CreateError(cDetalle_OC.Id_ProductoProperty, "El producto es requerido"));
}
if (!instance.Cantidad.HasValue || instance.Cantidad.Value<=0)
{
validationResults.Add(FieldValidationResult.CreateError(cDetalle_OC.CantidadProperty, "La cantidad tiene que ser mayor a 0"));
}
}
protected override void ValidateBusinessRules(cDetalle_OC instance, List<IBusinessRuleValidationResult> validationResults)
{
// No business rules validations yet
}
}
问题的原因是同时使用了 ValidatesOnDataErrors 和 NotifyOnValidationError。一次只能使用一个。