ItemsControl(ListBoxと言う)があり、その内容にDataTemplate
があります。ボタンを押すと、すべてのListBoxItemを検証します。これは機能します。itemscontrol内の項目のデータテンプレートの検証
しかし、すべてのアイテムが適切に検証されていて、エラーメッセージを取得することはできますが、WPFにはListBox
のSelectedItem
のValidationError.Template
が表示されます。検証に失敗した他の項目にはValidationError.Template
が表示されません。私はすべてのアイテムのバインディングのソースを更新し、Validation.HasErrorプロパティはtrueに設定されています!ビジュアルが欠けているだけで、スタイルは適用されていません!
誰にも問題の解決策がありますか?
サンプル
のTextBoxスタイル:個人エンティティの
<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>
AのDataTemplate:いくつかのコントロールで
<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>
次に、いくつかの人物を編集してみてください。たとえば、名前をnullに設定したり、誤ったバインディングを使用したりします。検証すると、Validation.HasError
のトリガーは選択した項目に対してのみ設定されます。
この問題を回避するにはどうすればよいですか?
これは問題とは全く関係ありません。データ・テンプレートは、Person-Entityのバリデーターを含める必要があります。エンティティ用のデータテンプレートを用意するのは問題ありません。 – Falcon