[英]No Style Inheritance Inside ItemsControl / DataTemplate in WPF?
can anyone explain why the TextBlock
inside my DataTemplate
does not apply the style defined in my UserControl.Resources
element, but the second TextBlock
('Test B') does?谁能解释为什么我的
DataTemplate
中的TextBlock
不应用我的UserControl.Resources
元素中定义的样式,但第二个TextBlock
('Test B')却应用了?
I think it may have to do with a dependency property somewhere set to not inherit, but I can't be sure.我认为这可能与某处设置为不继承的依赖属性有关,但我不能确定。
<UserControl.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Padding" Value="8 2" />
</Style>
</UserControl.Resources>
<StackPanel>
<ItemsControl ItemsSource="{Binding}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<!--Padding does not apply-->
<TextBlock>Test A</TextBlock>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!--Padding applies-->
<TextBlock>Test B</TextBlock>
</StackPanel>
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.模板中的元素位于此边界范围内,并在运行时查找具有匹配目标类型的样式在此范围内结束,因此外部的
TextBlock
将选取该样式而内部的则不会。 like adminSoftDK said you should give the style an x:Key and then apply it as static resource it will work
.就像 adminSoftDK 说你应该
give the style an x:Key and then apply it as static resource it will work
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.