列表框内的ListBoxItem没有完全拉伸到屏幕宽度

时间:2016-10-06 08:23:31

标签: c# xaml silverlight windows-phone-8 listbox

 <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有多个数据模板。纵向方向很好,但当我将其更改为横向时,listboxitem高度自动设置为MinHeight,宽度设置为{ {1}}但我希望宽度与我尝试以下的屏幕宽度相同

MinWidth

<ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch" /> </Style> </ListBox.ItemContainerStyle> 具有相同的宽度和高度,唯一的区别是我看到它与屏幕中心对齐。我该如何解决这个问题?Screen

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。该问题是由DataTemplateSelector引起的,您必须在Template部分中为DataTemplateSelector(StationTemplateSelector)设置<phone:PhoneApplicationPage.Resources>值才能使其生效。< / p>

<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>

还需要在ItemContainerStyle控件

中设置ListBox
<ListBox.ItemContainerStyle>
     <Style TargetType="ListBoxItem">
         <Setter Property="HorizontalContentAlignment" Value="Stretch" />
     </Style>
</ListBox.ItemContainerStyle>