我希望我的錯誤模板看起來不同,具體取決於裝飾控件上的某些屬性值。訪問AdornedElementPlaceholder屬性的Validation.ErrorTemplate
像下面那樣設置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>
我刪除了目標類型,然後嘗試這樣:
<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路徑錯誤:在‘對象’‘’AdornedElementPlaceholder「未找到‘IsReadOnly’屬性」:
<DataTrigger Binding="{Binding IsReadOnly, ElementName=placeholder}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
有沒有人對如何在ErrorTemplate引用依賴屬性的任何其他的想法?