如何设置WPF ListView行高?

34

我有一个ListView,里面显示了几个文本记录。我需要增加行高(因为我是在触摸屏上工作,所以我需要更厚的行),但不想增加字体大小。

这可能非常琐碎,但我一点头绪也没有,在谷歌上也找不到太多相关信息。

任何帮助都将不胜感激。

3个回答

80

通过使用 ItemContainerStyle,您可以设置 ListView 中所有 ListViewItems 的高度:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Height" Value="50" />
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

10

或者您可以使用样式来为所有列表视图设置它。这里将其作用域限定在窗口内:

<Window x:Class="WpfApplication2.Window1"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       Title="Window1" Height="300" Width="300">

    <Window.Resources>
        <Style TargetType="ListViewItem">
            <Setter Property="Height" Value="100"/>
        </Style>
    </Window.Resources>
    ...
</Window>

这实际上相当不错。 - Carl R

3
在XAML中
  <Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <StackPanel>
                <ListView x:Name="myListView">
                    <ListViewItem Height="50">Test</ListViewItem>
                    <ListViewItem Height="30">Test</ListViewItem>
                </ListView> 
            </StackPanel>
        </Grid>
    </Window>

在C#的代码后台

    foreach (ListViewItem lv in myListView.Items)
    {
        lv.Height = 30;
    }

希望您已经明白了这个想法。

ListViewItem没有Height属性。 - Tyler Pantuso

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接