WPF - How to apply a style to an AdornedElementPlaceholder AdornedElement?

I am trying to apply a style to a decorated element, but I do not know the correct syntax. Here is what I tried:

<!-- ValidationRule Based Validitaion Control Template --> <ControlTemplate x:Key="validationTemplate"> <DockPanel> <TextBlock Foreground="Red" FontSize="20">!</TextBlock> <AdornedElementPlaceholder Style="textStyleTextBox"/> </DockPanel> </ControlTemplate> 

The only problem is that the following line does not work:

  <AdornedElementPlaceholder Style="textStyleTextBox"/> 

Any help would be greatly appreciated.

Thanks,

-Charles

+4
source share
1 answer

You must specify where the resource is coming from.

 <TextBox Style="{StaticResource textStyleTextBox}"/> 

Then define a style in the resource, for example user management resources:

 <UserControl.Resources> <Style TargetType="TextBox" x:Key="textStyleTextBox"> <Setter Property="Background" Value="Blue"/> </Style> </UserControl.Resources> 

However, I do not believe that you want to set the style of the decorated element inside the placeholder. This is just a placeholder for any control with this template. You should set the style of the decorated element in the element itself, as in the example above. If you want to style a control based on its verification, then something like this:

 <Window.Resources> <ControlTemplate x:Key="validationTemplate"> <DockPanel> <TextBlock Foreground="Yellow" Width="55" FontSize="18">!</TextBlock> <AdornedElementPlaceholder/> </DockPanel> </ControlTemplate> <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}"> <Style.Triggers> <Trigger Property="Validation.HasError" Value="true"> <Setter Property="Background" Value="Red"/> <Setter Property="Foreground" Value="White"/> </Trigger> </Style.Triggers> </Style> </Window.Resources> <StackPanel x:Name="mainPanel"> <TextBlock>Age:</TextBlock> <TextBox x:Name="txtAge" Validation.ErrorTemplate="{DynamicResource validationTemplate}" Style="{StaticResource textBoxInError}"> <Binding Path="Age" UpdateSourceTrigger="PropertyChanged" > <Binding.ValidationRules> <ExceptionValidationRule/> </Binding.ValidationRules> </Binding> </TextBox> </StackPanel> 
+9
source

All Articles