wpf <ListBox.Resources> 或 <ListBox.ItemContainerStyle> 中的 ListBoxItem 样式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21799832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ListBoxItem style in <ListBox.Resources> or in <ListBox.ItemContainerStyle>?
提问by Gerard
I can put a xaml Stylefor ListBoxItemin <ListBox.Resources>or in <ListBox.ItemContainerStyle>. See code.
Question is: what is the difference, what should I prefer?
我可以把一个XAMLStyle为ListBoxItem中<ListBox.Resources>或<ListBox.ItemContainerStyle>。见代码。
问题是:有什么区别,我应该更喜欢什么?
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Top" Value="{Binding Top}"/>
<Setter Property="Canvas.Left" Value="{Binding Left}"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.Resources>
OR:
或者:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Top" Value="{Binding Top}"/>
<Setter Property="Canvas.Left" Value="{Binding Left}"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Padding" Value="0"/>
</Style>
</ListBox.ItemContainerStyle>
There is an answer that I accept, but behold and think about this strange symptom:
Either waygives me this strange databinding warning: Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null .... etc.
This is a binding that is hidden somewhere in the system Aero styles, it is not mine.
Only when I use both stylesthis warning disappears!
有一个我接受的答案,但请注意并考虑这个奇怪的症状:
无论哪种方式都给了我这个奇怪的数据绑定警告:无法找到带有引用的绑定源 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel ='1''。BindingExpression:Path=HorizontalContentAlignment; DataItem=null .... 等等
这是一个隐藏在系统 Aero 样式中某处的绑定,它不是我的。
只有当我同时使用这两种样式时,此警告才会消失!
回答by dev hedgehog
ItemContainerStyleis the right way to do this because its set explicitly and so wpf will not need to look up always where the style might be. Its faster and better. That is why that property is there.
ItemContainerStyle是执行此操作的正确方法,因为它明确设置,因此 wpf 不需要始终查找样式可能所在的位置。它更快更好。这就是为什么该财产存在的原因。
When ItemContainerStyleis not set WPF will seek for the style in ListBox.Resourcesor Window.Resourcesor Application.Resources. That is bad for performance.
当ItemContainerStyle未设置时,WPF 将在ListBox.ResourcesorWindow.Resources或 中寻找样式Application.Resources。这对性能不利。
回答by Rohit Vats
First is default Styleand second is Explicit style.
第一个是default Style,第二个是Explicit style。
First one will be applied to all ListBoxItemsfound under VisualTree of parent ListBox.
第一个将应用于ListBoxItems在父 ListBox 的 VisualTree 下找到的所有内容。
<ListBox>
<ListBox.Resources>
<!-- default Style -->
</ListBox.Resources>
<ListBox.ItemTemplate>
<DataTemplate>
<ListBox>
<ListBoxItem/> <-- Will be applied to this as well.
</ListBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
whereas second one will be applied to only ListBoxItemscorresponding to outer ListBox.
而第二个将仅应用于ListBoxItems对应于外部 ListBox。
Second is equivalent to:
第二个相当于:
<Grid>
<Grid.Resources>
<Style x:Key="MyStyle" TargetType="ListBoxItem"/>
</Grid.Resources>
<ListBox ItemContainerStyle="{StaticResource MyStyle}"/>
</Grid>
If you look at the default style declared under PresentationFramework.Aero.dllvia ILSpy, you can see these two setters over there:
如果您查看PresentationFramework.Aero.dll通过 ILSpy声明的默认样式,您可以在那里看到这两个 setter:
<Setter Property="HorizontalContentAlignment"
Value="{Binding Path=HorizontalContentAlignment,
RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment"
Value="{Binding Path=VerticalContentAlignment,
RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
So, when you set that in your default style declared under resource, it overrides those settersand hence error goes away.
因此,当您在资源下声明的默认样式中设置它时,它会覆盖这些设置器,因此错误消失。
But, in case you set it on style declared inline of ItemContainerStylethat error won't go away since it still refer to the default style. In case you don't want to basedOn default style set BasedOnattribute to x:Null.
但是,如果您将它设置在ItemContainerStyle该错误的内联声明的样式上,它不会消失,因为它仍然引用默认样式。如果您不想将 basedOn 默认样式设置BasedOn为x:Null.
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{x:Null}"/>
</ListBox.ItemContainerStyle>
</ListBox>

