ck84vi
ck84vi

Reputation: 1576

How to Resize ListView control when resizing window

I have a user control that holds a ListView within a grid. But my problem is that if i maximize my applications window the ListView dimension (Height) does not change relative to the window. It always keeps the same height. In this case 200 as defined below.

<UserControl x:Class="ModuleLogger.Views.ViewLogger"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" >
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="15" />
            <RowDefinition Height="30" />
            <RowDefinition Height="15" />
            <RowDefinition Height="1*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="1" Grid.Column="0" Content="Log Entries" Style="{StaticResource myLblStatHeaderSmallStyle}"  HorizontalAlignment="Stretch" Grid.ColumnSpan="6" />
        <ListView x:Name="lvLogs"  Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" ItemsSource="{Binding LogEntries}" Style="{StaticResource myListViewStyle}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Log Entry" Width="810">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock TextWrapping="Wrap" Text="{Binding Path=Entry}"></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Grid>
</UserControl>

<Style x:Key="myListViewStyle" TargetType="{x:Type ListView}">
    <Setter Property="Background" Value="WhiteSmoke"/>
    <Setter Property="BorderBrush" Value="Gray"/>
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Margin" Value="5,5,5,5"/>
    <Setter Property="Focusable" Value="False"/>
    <Setter Property="Foreground" Value="Black"/>
    <Setter Property="FontWeight" Value="Normal"/>
    <Setter Property="VerticalContentAlignment" Value="Center"/>
    <Setter Property="HorizontalContentAlignment" Value="Center"/>
</Style>

The user control itself is used within a tabitem.

<TabItem Header="Log" Name="tabLog" BorderBrush="LightGray" Style="{StaticResource myMainTabItemStyle}" VerticalContentAlignment="Stretch">
    <ItemsControl Name="RegionLog" prism:RegionManager.RegionName="RegionLog" Grid.Column="0"/>
</TabItem>

I have tried to change my RowDefinition to

<Grid.RowDefinitions>
     <RowDefinition Height="15" />
     <RowDefinition Height="30" />
     <RowDefinition Height="15" />
     <RowDefinition Height="1*" />
</Grid.RowDefinitions>

This approach solves my problem. But it brings me to another problem. In this case i dont get a vertical scrollbar. So the ListView becomes "endless" and you can just see the top entries.

Is there a way to define the Grid//ListView so that the ListView automatically resizes if the window resizes?

Upvotes: 2

Views: 5800

Answers (1)

ck84vi
ck84vi

Reputation: 1576

Finally i could solve the problem. The key is the information from following link: ItemsControl takes as much space as it needs for its child elements, which can cause it to be larger than the screen. Because its size is based on its child controls, ItemsControl does not resize when re-sizing the screen.

So the following xaml part was causing the problem.

<TabItem Header="Log" Name="tabLog" BorderBrush="LightGray" Style="{StaticResource myMainTabItemStyle}" VerticalContentAlignment="Stretch">
    <ItemsControl Name="RegionLog" prism:RegionManager.RegionName="RegionLog" Grid.Column="0"/>
</TabItem>

Instead of using a ItemsControl I use a ContentControl.

<TabItem Header="Log" Name="tabLog" BorderBrush="LightGray" Style="{StaticResource myMainTabItemStyle}" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
    <ContentControl Name="RegionLog" prism:RegionManager.RegionName="RegionLog" Grid.Column="0"/>
</TabItem>

So my ListView just expands to the maximum size that is given by the ContentControl.

Maybe that helps somebody in the future who runs into the same problem.

Upvotes: 2

Related Questions