Silverlight 3:ListBox DataTemplate Horizo​​ntalAlignment

编程入门 行业动态 更新时间:2024-10-20 20:39:03
本文介绍了Silverlight 3:ListBox DataTemplate Horizo​​ntalAlignment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个ListBox,它的ItemTemplate绑定到DataTemplate.我的问题是我无法使模板中的元素延伸到ListBox的整个宽度.

I have a ListBox with it's ItemTemplate bound to a DataTemplate. My problem is I cannot get the elements in the template to stretch to the full width of the ListBox.

<ListBox x:Name="listPeople" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,0,0,0" Background="{x:Null}" SelectionMode="Extended" Grid.Row="1" ItemTemplate="{StaticResource PersonViewModel.BrowserDataTemplate}" ItemsSource="{Binding Mode=OneWay, Path=SearchResults}" > </ListBox> <DataTemplate x:Key="PersonViewModel.BrowserDataTemplate"> <ListBoxItem HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="5,5,5,5"> <Border Opacity=".1" x:Name="itemBorder" Background="#FF000000" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" CornerRadius="5,5,5,5" MinWidth="100" Height="50"/> </Grid> </ListBoxItem> </DataTemplate>

如您所见,我在网格内添加了边框以指示模板的宽度.我的目标是看到此边框扩展到列表框的整个宽度.当前,它的宽度由其内容或MinWidth决定,这是目前唯一保持可见的东西.

As you can see, I have added a border within the grid to indicate the width of the template. My goal is to see this border expand to the full width of the listbox. Currently its width is determined by its contents or MinWidth, which is the only thing at the moment keeping it visible at all.

推荐答案

为ListBox创建数据模板时,不应包含<ListBoxItem>. DataTemplate的内容将放置在生成的容器内.您可以使用ItemContainerStyle来控制该容器的构造方式.

When creating Data Templates for ListBox, you should not incldue <ListBoxItem>. The contents of the DataTemplate will be placed inside of a generated container. You can control how that container is constructed using ItemContainerStyle.

默认情况下,ListBoxItem的默认控件样式用于定义ItemContainerStyle.此样式将ListBoxItem.Horizo​​ntalContentAlignment属性设置为左".请注意ContentPresenter如何将其Horizo​​ntalAlignment绑定到此属性.

The default control style for ListBoxItem is used to define the ItemContainerStyle by default. This style sets the ListBoxItem.HorizontalContentAlignment property to 'Left'. Notice how the ContentPresenter binds its HorizontalAlignment to this property.

您需要覆盖绑定到ListBox时正在生成的ListBoxItem容器的样式.这可以通过设置ItemContainerStyle来完成.将Horizo​​ntalContentAlignment属性设置为拉伸".

You need to override the style of the ListBoxItem container that is being generated when you bind to your ListBox. This can be done by setting the ItemContainerStyle. Set the HorizontalContentAlignment property to be "Stretch".

下面是默认的ListBoxItem样式.包括以供参考.

Below is the default ListBoxItem Style. Included for reference.

<Style x:Key="ListBoxItemStyle1" TargetType="ListBoxItem"> <Setter Property="Padding" Value="3"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="VerticalContentAlignment" Value="Top"/> <Setter Property="Background" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="TabNavigation" Value="Local"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <Grid Background="{TemplateBinding Background}"> <!-- VSM excluded for readability --> <Rectangle x:Name="fillColor" Fill="#FFBADDE9" RadiusX="1" RadiusY="1" IsHitTestVisible="False" Opacity="0"/> <Rectangle x:Name="fillColor2" Fill="#FFBADDE9" RadiusX="1" RadiusY="1" IsHitTestVisible="False" Opacity="0"/> <ContentPresenter x:Name="contentPresenter" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> <Rectangle x:Name="FocusVisualElement" Stroke="#FF6DBDD1" StrokeThickness="1" RadiusX="1" RadiusY="1" Visibility="Collapsed"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>

更多推荐

Silverlight 3:ListBox DataTemplate Horizo​​ntalAlignment

本文发布于:2023-11-07 05:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565680.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ListBox   Silverlight   DataTemplate   ntalAlignment   Horizo

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!