DonBoitnott
DonBoitnott

Reputation: 11035

Why does my style not get picked up?

I am trying to apply a style to all FrameworkElement objects via a Style defined in a ResourceDictionary. In one particular view, I've included the resource dictionary and there's a TextBox on it that I want to use that style.

So, here are the two definitions I have played around with:

    <!-- STYLE USED BY ALL FRAMEWORK ELEMENTS TO DISPLAY TOOLTIP ON VALIDATION ERROR -->
    <Style TargetType="{x:Type FrameworkElement}">
        <Style.Triggers>
            <Trigger Property="Validation.HasError"
                     Value="True">
                <Setter Property="ToolTip"
                        Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors).CurrentItem.ErrorContent}" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <!-- STYLE USED BY ALL TEXTBOX CONTROLS FOR VALIDATION ERROR DISPLAY -->
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <DockPanel LastChildFill="True">
                        <Image DockPanel.Dock="Right"
                               Source="{StaticResource imgDisallow16}" Width="16"/>
                        <Border BorderBrush="Red"
                                BorderThickness="1">
                            <AdornedElementPlaceholder />
                        </Border>
                    </DockPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

As written, the style for the Tooltip doesn't kick in. If I give that style a x:Key and reference that name directly on the TextBox, it works. But why doesn't the FrameworkElement type do it? Since TextBox inherits from it, that is.

Likewise, if I add that Tooltip trigger to the second style, that works. That one does not have a name, but targets TextBox. So why does that work without a name, but the first does not?

EDIT: here's the `TextBox

<TextBox Grid.Row="0"
     Grid.Column="2">
    <TextBox.Text>
    <Binding Path="CurrentEquipment.Name"
         Mode="TwoWay"
         ValidatesOnNotifyDataErrors="True"
         NotifyOnValidationError="True"
         UpdateSourceTrigger="PropertyChanged">
        <Binding.ValidationRules>
            <vr:EmailValidationRule />
        </Binding.ValidationRules>
    </Binding>
    </TextBox.Text>
</TextBox>

Upvotes: 0

Views: 37

Answers (1)

mm8
mm8

Reputation: 169390

The TextBox style doesn't extend the FrameworkElement style unless you explicitly base the former on the latter:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type FrameworkElement}}">
    ...
</Style>

Upvotes: 1

Related Questions