ListBoxItem HorizontalContentAlignment To Stretch Across Full Width of ListBox

蹲街弑〆低调 提交于 2019-11-27 23:51:15

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!