maxp maxp - 1 year ago 140
C# Question

No Style Inheritance Inside ItemsControl / DataTemplate in WPF?

can anyone explain why the

inside my
does not apply the style defined in my
element, but the second
('Test B') does?

I think it may have to do with a dependency property somewhere set to not inherit, but I can't be sure.

<Style TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="8 2" />
<ItemsControl ItemsSource="{Binding}">
<!--Padding does not apply-->
<TextBlock>Test A</TextBlock>
<!--Padding applies-->
<TextBlock>Test B</TextBlock>

Answer Source

Templates are considered as a boundary. Elements within the templates falls in this boundary range, and look up for the style with a matching target type ends within this range at runtime as a result the TextBlock outside will pickup the style and the one inside wont. like adminSoftDK said you should give the style an x:Key and then apply it as static resource it will work. Check this.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download