当窗口大小更改时,WPF在C#中获取ListView高度

时间:2014-04-22 14:25:49

标签: c# wpf listview height

我有以下问题:
我有一个Grid和ListViews的布局 这些ListView将始终包含5个项目 现在这些项目应该填充ListView的完整高度 所以我想我得到了ListView的高度,然后将它除以5,并将每行的高度设置为结果。
但ListView.Height属性返回奇怪的值 我听Window SizeChanged事件。
我希望有人可以告诉我该怎么做,或者是否有更好的选择。

<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>

这是我背后的代码:

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

1 个答案:

答案 0 :(得分:0)

我会这样做:

int wHeight = (int)lvMoN.ActualHeight;