ListBoxItem HorizontalContentAlignment To Stretch Across Full Width of ListBox

前端 未结 1 1902
借酒劲吻你
借酒劲吻你 2020-12-06 11:50

I have a problem with my ListBoxItem\'s on a Windows Phone 8 app, while trying to get them to stretch across all the width of the ListBox.

相关标签:
1条回答
  • 2020-12-06 12:26

    I ran into the same in XAML and it drove me nuts wondering why my TextBlock was not fully colored across the width.

    The way to work with the competing styles (this works for any of the xaml variants actually) is to define style of the ListBoxItem explicitly to handle the space usage.

    That gives the xaml a hint that it is to fill in (stretch) to the screen area in this way:

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

    Otherwise the xaml parser, by default, tries to conserve space by auto sizing it to the contents of the ListBoxItem; giving it that dreaded scotch tape look.

    0 讨论(0)
提交回复
热议问题