代码之家  ›  专栏  ›  技术社区  ›  Vishnu Babu

列表框中的ListBoxItem未完全延伸到屏幕宽度

  •  0
  • Vishnu Babu  · 技术社区  · 8 年前
     <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 和宽度 MinWidth 但是我希望宽度与屏幕宽度相同

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

    但是 ListBoxItem 具有相同的宽度和高度,唯一的区别是它与屏幕中心对齐。我怎样才能解决这个问题? Screen

    1 回复  |  直到 8 年前
        1
  •  0
  •   Vishnu Babu    8 年前

    好吧,我找到了解决办法。该问题是由 DataTemplateSelector Template 价值 DataTemplateSelector (StationTemplateSelector)您在节中使用的 <phone:PhoneApplicationPage.Resources> 为了让它发挥作用。

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