How to add a DataTrigger to a ControlTemplate with binding to the TemplateParent property? I want to change the Margin property of the Border depending on a specific property of the ComboBox. This ControlTemplate I use as the Validation.ErrorTemplate.
<ControlTemplate x:Key="val">
<Border Name="bor">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<AdornedElementPlaceholder x:Name="customAdorner"
Grid.Column="0"
VerticalAlignment="Center" />
<Border Grid.Column="0" x:Name="customBorder"
Width="15"
Height="15"
Margin="0,0,15,0"
Background="Red"
CornerRadius="8"
HorizontalAlignment="Right"
ToolTip="{Binding ElementName=customAdorner,
Path=AdornedElement.(Validation.Errors)[0].ErrorContent}">
<TextBlock HorizontalAlignment="center"
VerticalAlignment="center"
FontWeight="Bold"
Foreground="white"
Text="!" />
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding SomeProperrty, RelativeSource={RelativeSource AncestorType=ComboBox}}" Value="value">
<Setter Property="Margin" TargetName="customBorder" Value="0,0,20,0"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
The validation error template is not part of the visual tree. Instead, it is rendered on the adorner layer. However, you can find the adorned element by binding to the parent
Adornerthat hosts the error template. The adorned element is stored in theAdorner.AdornedElementproperty. From there you can also access the original visual tree that contains the adorned element, if required.