views:

1419

answers:

1

I'm trying to apply a style to an adorned element, but I don't know the correct syntax. Here is what I've 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 doesn't work:

            <AdornedElementPlaceholder Style="textStyleTextBox"/>

Any help would be greatly appreciated.

Thanks,

-Charles

+4  A: 

Need to put where the resource is coming from.

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

Then define the style in a resource such as the user control resources:

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

However I dont believe you want to set the style of the adornedelement within the placeholder. It's just a placeholder for any control with that template. You should set the style of the adornedelement in the element itself like the example I provided above. If you want to style the control based upon it's validation 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>
Crippeoblade
Thanks, that makes sense. I was hoping there would be a way to state in the ControlTemplate that I wanted to use my 'textStyleTextBox' when an error occurs, as opposed to setting both 'Validation.ErrorTemplate' and 'Style' on every textbox that needs validation. Can you think of any way to do that?
Charles
You can use styles at application level (Application.Resources) without a key such as a textblock style and all textblocks will automatically pick that style up by default.
Crippeoblade
Crippeoblade