Stretch ListBox Items hit area to full width of the ListBox? ListBox style is set implicity through a theme

后端 未结 2 1235
一整个雨季
一整个雨季 2021-01-02 00:44

I\'ve looked around for an answer on this, but the potential duplicates are more concerned with presentation than interaction.

I have a basic list box, and each ite

相关标签:
2条回答
  • 2021-01-02 01:27

    HorizontalContentAlignment="Stretch" should be set in ItemContainerStyle for this to work.

    Xaml Example

    <ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <ListBox.ItemsSource>
            <x:Array Type="{x:Type sys:String}">
                <sys:String>String 1</sys:String>
                <sys:String>String 2</sys:String>
                <sys:String>String 3</sys:String>
                <sys:String>String 4</sys:String>
            </x:Array>
        </ListBox.ItemsSource>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding}" Background="Green"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
    

    Update
    Try this

    <ListBox ...>
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <Setter Property="HorizontalContentAlignment" Value="Stretch" />
            </Style>
        </ListBox.ItemContainerStyle>
    
    0 讨论(0)
  • 2021-01-02 01:39

    Also, adding to Fredrik's answer, if you set the ListBox's ScrollViewer.HorizontalScrollBarVisibility property to Disabled, the items will always have exactly the client width of the ListBox.

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