【发布时间】:2014-12-01 00:30:49
【问题描述】:
我想让我的错误模板看起来有所不同,具体取决于装饰控件上的某些属性值。
如下设置 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 路径错误:在 'object' ''AdornedElementPlaceholder' 上找不到 'IsReadOnly' 属性”:
<DataTrigger Binding="{Binding IsReadOnly, ElementName=placeholder}" Value="True">
<Setter Property="Background" Value="Green"/>
</DataTrigger>
有没有人对如何在 ErrorTemplate 中引用依赖属性有任何其他想法?
【问题讨论】:
标签: wpf validation xaml binding errortemplate