列表框内的 ListBoxItem 未完全拉伸到屏幕宽度
ListBoxItem inside a listbox not stretching completly to screen width
<Grid Name="FavouriteStations" Visibility="Visible" >
<ListBox Name="FavouriteStationsListBox" ItemsSource="{Binding}" Loaded="FavouriteStationsListBoxLoaded" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:StationTemplateSelector Content="{Binding}">
<local:StationTemplateSelector.St>
<DataTemplate>
<Grid MinHeight="150" MinWidth="480" Background="#242224" >
</Grid>
</DataTemplate>
</local:StationTemplateSelector.St>
<local:StationTemplateSelector.Ad>
<DataTemplate>
<Grid MinHeight="150" MinWidth="480" Background="#FFFFFF" >
</Grid>
</DataTemplate>
</local:StationTemplateSelector.Ad>
</local:StationTemplateSelector>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
我有一个包含多个数据的 listbox
templates.It 在纵向方向上没问题,但是当我将其更改为横向时 listboxitem
高度自动设置为 MinHeight
和宽度 MinWidth
但我希望宽度与屏幕宽度相同 我尝试了以下
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
但是 ListBoxItem
具有相同的宽度和高度,我看到的唯一区别是它与屏幕中心对齐。我该如何解决这个问题?
好的,我找到了解决办法。该问题是由 DataTemplateSelector
引起的,您必须为您在 <phone:PhoneApplicationPage.Resources>
部分中使用的 DataTemplateSelector
(StationTemplateSelector) 设置 Template
值才能正常工作。
<phone:PhoneApplicationPage.Resources>
<Style TargetType="local:StationTemplateSelector">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:StationTemplateSelector">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
</Style>
还需要在 ListBox
控件中设置 ItemContainerStyle
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<Grid Name="FavouriteStations" Visibility="Visible" >
<ListBox Name="FavouriteStationsListBox" ItemsSource="{Binding}" Loaded="FavouriteStationsListBoxLoaded" >
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<local:StationTemplateSelector Content="{Binding}">
<local:StationTemplateSelector.St>
<DataTemplate>
<Grid MinHeight="150" MinWidth="480" Background="#242224" >
</Grid>
</DataTemplate>
</local:StationTemplateSelector.St>
<local:StationTemplateSelector.Ad>
<DataTemplate>
<Grid MinHeight="150" MinWidth="480" Background="#FFFFFF" >
</Grid>
</DataTemplate>
</local:StationTemplateSelector.Ad>
</local:StationTemplateSelector>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
我有一个包含多个数据的 listbox
templates.It 在纵向方向上没问题,但是当我将其更改为横向时 listboxitem
高度自动设置为 MinHeight
和宽度 MinWidth
但我希望宽度与屏幕宽度相同 我尝试了以下
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
但是 ListBoxItem
具有相同的宽度和高度,我看到的唯一区别是它与屏幕中心对齐。我该如何解决这个问题?
好的,我找到了解决办法。该问题是由 DataTemplateSelector
引起的,您必须为您在 <phone:PhoneApplicationPage.Resources>
部分中使用的 DataTemplateSelector
(StationTemplateSelector) 设置 Template
值才能正常工作。
<phone:PhoneApplicationPage.Resources>
<Style TargetType="local:StationTemplateSelector">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:StationTemplateSelector">
<ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
</Style>
还需要在 ListBox
控件中设置 ItemContainerStyle
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>