How can i make a ListView's items take up all the available height?

时间:2016-04-07 10:37:59

标签: c# wpf xaml

I am trying to make a column chart using WPF. For the Y axis I have a list of values which I would like to display on the control.

I have a ListView bound to the collection. How can I get the items to spread over the entire length of the list view rather then squish together at the top? And is the ListViewthe correct control to use for this?

Here is a sample of my Xaml:

<ListView Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" 
          ItemsSource="{Binding YAxisValues}" 
          Background="Gray" VerticalContentAlignment="Stretch">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Label Content="{Binding Path=Value}" 
                       Foreground="White" 
                       HorizontalAlignment="Left" 
                       FontSize="12" Width="50"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Desired ListView

On the left is the list view as i currently have it. On the right is my desired result.

How can I change my ListView to get there? Or do I need to use a different control entirely?

2 个答案:

答案 0 :(得分:2)

使用UniformGrid作为ItemsPanel项目获得相等的高度

<ListView Name="Axis" Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" 
        ItemsSource="{Binding YAxisValues}"
        Background="Gray" VerticalContentAlignment="Stretch"              
        ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="1"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <!--no changes-->
    </ListView.ItemTemplate>
</ListView>

当ListView没有足够的高度用于所有元素时,可能会出现问题。在这种情况下,我会将ListView放入ViewBox

答案 1 :(得分:0)

Set HorizontalContentAlignment to Stretch for items.

       <ListView>
            <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>
        </ListView>