我定义了一个样式,让所有的StackPanel都是绿色的:
<Window.Resources>
<Style TargetType="StackPanel">
<Setter Property="Background" Value="Green" />
</Style>
</Window.Resources>但是如果我使用StackPanel作为面板模板,那么它就不是绿色的:
<UniformGrid>
<StackPanel /><!-- this one is green -->
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel /><!-- this one is not -->
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</UniformGrid>为什么?怎样才能让它也变绿呢?
发布于 2019-10-15 21:45:58
将隐式Style移动到App.xaml,或将基于隐式Style的资源添加到ItemsPanelTemplate
<ItemsControl>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<ItemsPanelTemplate.Resources>
<Style TargetType="StackPanel" BasedOn="{StaticResource {x:Type StackPanel}}" />
</ItemsPanelTemplate.Resources>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>如果您不执行这些操作,不是从Control继承的类型将不会获得隐式样式。
https://stackoverflow.com/questions/58395780
复制相似问题