我有一个ItemsControl(我们说一个ListBox),并且我有一个DataTemplate
的内容。当我按下按钮时,我想要验证所有ListBoxItems。这工作。验证项目控件中的项目的数据模板
然而,尽管所有项目都经过了适当的验证,并且我可以为它们检索错误消息,但WPF仅为ListBox
的SelectedItem
显示ValidationError.Template
。它不会显示验证失败的其他项目的ValidationError.Template
。我会更新每个项目的绑定源,并且Validation.HasError属性对它们设置为true!只是视觉缺失,风格没有被应用!
有没有人有问题的解决方案?
样品
一个文本框风格:
<Style TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel LastChildFill="True">
<AdornedElementPlaceholder Name="MyAdorner" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="{DynamicResource TextBoxBackgroundBrush}" />
<Style.Triggers>
<Trigger Property="IsFocused" Value="true">
<Setter Property="Background" Value="{DynamicResource TextBoxFocusBackgroundBrush}" />
</Trigger>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="{DynamicResource ErrorBrush}" />
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
一个DataTemplate一个Person实体:
<DataTemplate DataType="{x:Type entities:Person}" x:Key="PersonItemStyle">
<Grid>
<TextBox x:Name="SomeTextBox">
<TextBox.Text>
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<validators:RequiredFieldValidationRule ErrorMessage="Please enter a name!" />
</Binding.ValidationRules/>
</Binding>
</TextBox.Text>
</TextBox>
</Grid>
</DataTemplate>
某处在一定的控制:
<ListBox Grid.Row="1" x:Name="ListBoxPersons" Style="{DynamicResource ListBoxStyle}" ItemsSource="{Binding Path=Persons}"
ItemContainerStyle="{StaticResource PersonItemStyle}">
</ListBox>
然后尝试编辑几个人,例如将其名称设置为空或使用任何错误的绑定。验证时,只会为所选项目设置触发器Validation.HasError
。
如何解决这个问题?
这与问题完全无关。数据模板需要包含Person-Entity的验证器。拥有实体的数据模板非常好。 – Falcon