装飾されたコントロールのいくつかのプロパティ値に応じて、エラー テンプレートの外観を変えたいと思います。
以下のように TargetType を設定すると、実行時例外が発生します: 'TextBox' ControlTemplate TargetType は、テンプレート化された型 'Control' と一致しません。したがって、ErrorTemplate は「Control」の targetType を使用する必要があるようです。
<ControlTemplate x:Key="ValidationErrorTemplate" TargetType={x:Type TextBox}>
<Grid>
<AdornedElementPlaceholder HorizontalAlignment="Left" Name="placeholder"/>
<Grid Background="Yellow">
<Grid.Style>
<Style TargetType="Grid">
<Style.Triggers>
<DataTrigger Binding="{TemplateBinding IsReadOnly}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Grid.Style>
</Grid>
</Grid>
</ControlTemplate>
targetType を削除してから、これを試しました:
<DataTrigger Binding="{Binding IsReadOnly, RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
そして、これは例外も効果もありませんでした:
<DataTrigger Binding="{Binding AdornedElement.(TextBox.IsReadOnly), ElementName=placeholder}" Value="True">
<Setter Property="Background" Value="Orange"/>
</DataTrigger>
そして、これは例外も効果もありませんでした:
<DataTrigger Binding="{Binding (TextBox.IsReadOnly), ElementName=placeholder}" Value="True">
<Setter Property="Background" Value="Orange"/>
</DataTrigger>
そして最後に、「BindingExpression パス エラー: 'IsReadOnly' プロパティが 'object' ''AdornedElementPlaceholder' に見つかりませんでした」:
<DataTrigger Binding="{Binding IsReadOnly, ElementName=placeholder}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
ErrorTemplate で依存関係プロパティを参照する方法について、他のアイデアはありますか?