Question

I have the following Question:
I have a Layout with a Grid and ListViews.
These ListViews will always contain 5 Items.
Now these Items should fill the complete Height of the ListView.
So I thought I get the Height of the ListView an then devide it by 5 and set the Height of each Row to the Result.
But the ListView.Height Property returns strange Values.
I listen to the Window SizeChanged Event.
I hope somebody can tell me how to do it, or if there is a better alternative.

<Window x:Class="KalenderDesingTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Kalender" Height="600" Width="800" SizeChanged="Window_SizeChanged">
  <Grid>
     <Grid.Resources>
        <Style TargetType="{x:Type ListViewItem}">                                                                
            <Style.Setters>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListViewItem}">                                                                                                                                                
                            <Border BorderBrush="#5076A7" BorderThickness="1" CornerRadius="4">
                                <Border.Background>
                                    <LinearGradientBrush StartPoint="0,0" EndPoint="0,1">
                                        <GradientStop Color="#FFFFFF" Offset="0.0"/>
                                        <GradientStop Color="#C0D3EA" Offset="1.0"/>
                                    </LinearGradientBrush>
                                </Border.Background>
                                <StackPanel TextElement.FontFamily="Segoe UI" 
                                            TextElement.FontSize="12">
                                    <TextBlock FontWeight="Bold" Padding="3,0,0,0" Text="{Binding Path=Name}"/>
                                    <TextBlock Padding="3,0,0,0" Text="{Binding Path=Age}"/>                                                                                                                                                                                                                                                                                                                                
                                </StackPanel>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>                                                                                                
            </Style.Setters>
        </Style>
    </Grid.Resources>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="50"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Grid.Column="0" Grid.Row="0" 
            Grid.RowSpan="3" x:Name="NeuerTermin">Neuer Termin</Button>
    <Button Grid.Column="2" Grid.Row="0" x:Name="Back">Zurück</Button>
    <ComboBox Grid.Column="3" Grid.Row="0" x:Name="Month"></ComboBox>
    <Button Grid.Column="4" Grid.Row="0" x:Name="Forward">Vor</Button>

    <TextBlock Grid.Column="1" Grid.Row="1" Text="Montag" HorizontalAlignment="Center"/>
    <TextBlock Grid.Column="2" Grid.Row="1" Text="Dienstag" HorizontalAlignment="Center"/>
    <TextBlock Grid.Column="3" Grid.Row="1" Text="Mittwoch" HorizontalAlignment="Center"/>
    <TextBlock Grid.Column="4" Grid.Row="1" Text="Donnerstag" HorizontalAlignment="Center"/>
    <TextBlock Grid.Column="5" Grid.Row="1" Text="Freitag" HorizontalAlignment="Center"/>

    <StackPanel Orientation="Horizontal" Grid.Column="1" 
                Grid.Row="2" VerticalAlignment="Stretch">
        <CheckBox x:Name="MoV" VerticalAlignment="Center" Margin="0,0,5,0">V</CheckBox>
        <CheckBox x:Name="MoN" VerticalAlignment="Center" Margin="0,0,5,0">N</CheckBox>
        <CheckBox x:Name="MoT" VerticalAlignment="Center">T</CheckBox>
    </StackPanel>
    <StackPanel Orientation="Horizontal" Grid.Column="2" 
                Grid.Row="2" VerticalAlignment="Stretch">
        <CheckBox x:Name="DiV" VerticalAlignment="Center" Margin="0,0,5,0">V</CheckBox>
        <CheckBox x:Name="DiN" VerticalAlignment="Center" Margin="0,0,5,0">N</CheckBox>
        <CheckBox x:Name="DiT" VerticalAlignment="Center">T</CheckBox>
    </StackPanel>
    <StackPanel Orientation="Horizontal" Grid.Column="3" 
                Grid.Row="2" VerticalAlignment="Stretch">
        <CheckBox x:Name="MiV" VerticalAlignment="Center" Margin="0,0,5,0">V</CheckBox>
        <CheckBox x:Name="MiN" VerticalAlignment="Center" Margin="0,0,5,0">N</CheckBox>
        <CheckBox x:Name="MiT" VerticalAlignment="Center">T</CheckBox>
    </StackPanel>
    <StackPanel Orientation="Horizontal" Grid.Column="4" 
                Grid.Row="2" VerticalAlignment="Stretch">
        <CheckBox x:Name="DoV" VerticalAlignment="Center" Margin="0,0,5,0">V</CheckBox>
        <CheckBox x:Name="DoN" VerticalAlignment="Center" Margin="0,0,5,0">N</CheckBox>
        <CheckBox x:Name="DoT" VerticalAlignment="Center">T</CheckBox>
    </StackPanel>
    <StackPanel Orientation="Horizontal" Grid.Column="5" 
                Grid.Row="2" VerticalAlignment="Stretch">
        <CheckBox x:Name="FrV" VerticalAlignment="Center" Margin="0,0,5,0">V</CheckBox>
        <CheckBox x:Name="FrN" VerticalAlignment="Center" Margin="0,0,5,0">N</CheckBox>
        <CheckBox x:Name="FrT" VerticalAlignment="Center">T</CheckBox>
    </StackPanel>

    <ListView Grid.Column="1" Grid.Row="3" x:Name="lvMoV"></ListView>
    <ListView Grid.Column="1" Grid.Row="4" x:Name="lvMoN"></ListView>                                
    <ListView Grid.Column="2" Grid.Row="3" x:Name="lvDiV"></ListView>
    <ListView Grid.Column="2" Grid.Row="4" x:Name="lvDiN"></ListView>
    <ListView Grid.Column="3" Grid.Row="3" x:Name="lvMiV"></ListView>
    <ListView Grid.Column="3" Grid.Row="4" x:Name="lvMiN"></ListView>
    <ListView Grid.Column="4" Grid.Row="3" x:Name="lvDoV"></ListView>
    <ListView Grid.Column="4" Grid.Row="4" x:Name="lvDoN"></ListView>
    <ListView Grid.Column="5" Grid.Row="3" x:Name="lvFrV"></ListView>
    <ListView Grid.Column="5" Grid.Row="4" x:Name="lvFrN"></ListView>
  </Grid>
</Window>

This is my code behind:

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    int wHeight = (int)lvMoN.Height;
}
Was it helpful?

Solution

I would do like this:

int wHeight = (int)lvMoN.ActualHeight;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top