Search code examples
xamlwinui-3

Stretch list view to match expanders width


How would I go about setting the width of the list view to be the same as the width of the expander that it is inside?

<Expander x:Name="expander"
              IsExpanded="False" 
              ExpandDirection="Down" 
              Header="Main" 
              HorizontalAlignment="Stretch" 
              Grid.Column="1"
              Grid.Row="1">
            <Expander.Content>
                <ListView x:Name="listView"
                          SelectionMode="None"
                          IsItemClickEnabled="True"
                          HorizontalAlignment="Stretch"
                          ItemClick="ListItemClicked"
                          ItemTemplate="{StaticResource ListViewTemplate}"/>
            </Expander.Content>
</Expander>

Solution

  • Add HorizontalContentAligment="Stretch" to the Expander like this.

    <Expander
        x:Name="expander"
        Grid.Row="1"
        Grid.Column="1"
        HorizontalAlignment="Stretch"
    
        HorizontalContentAlignment="Stretch"
    
        ExpandDirection="Down"
        Header="Main"
        IsExpanded="False">
        <Expander.Content>
            <ListView
                x:Name="listView"
                HorizontalAlignment="Stretch"
                IsItemClickEnabled="True"
                SelectionMode="None" >
                <ListViewItem Content="Test"/>
                </ListView>
        </Expander.Content>
    </Expander>