Monstieur
Monstieur

Reputation: 8102

Style BasedOn suddenly not working

These simple styles just stopped working out of the blue. They worked fine until today.

<Style x:Key="textColumnElementStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
    <Setter Property="Padding" Value="5,1" />
</Style>

<Style x:Key="textColumnEditingElementStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Padding" Value="2,0" />
</Style>

These both shows errors on the BasedOn property.

The resource "{x:Type TextBlock}" could not be resolved.
The resource "{x:Type TextBox}" could not be resolved.

If I copy and pase one of the styles right next to itself, there is no error on the pasted style.

<Style x:Key="noErrorOnThisStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
    <Setter Property="Padding" Value="5,1" />
</Style>

<Style x:Key="textColumnElementStyle" TargetType="{x:Type TextBlock}" BasedOn="{StaticResource {x:Type TextBlock}}">
    <Setter Property="Padding" Value="5,1" />
</Style>

<Style x:Key="textColumnEditingElementStyle" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
    <Setter Property="Padding" Value="2,0" />
</Style>

Upvotes: 4

Views: 1614

Answers (1)

Claude Catonio
Claude Catonio

Reputation: 851

In fact in your case the BasedOn attribute is not needed. Just write

<Style x:Key="textColumnElementStyle" TargetType="{x:Type TextBlock}">
    <Setter Property="Padding" Value="5,1" />
</Style>

<Style x:Key="textColumnEditingElementStyle" TargetType="{x:Type TextBox}">
    <Setter Property="Padding" Value="2,0" />
</Style>

When BasedOn is not set, BasedOn point to the default style of the type specified by the TargetType attribute.

Regards

Claude

Upvotes: 1

Related Questions