如何设置 WPF ListView 行高?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1244090/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:49:12  来源:igfitidea点击:

How to set WPF ListView row height?

wpflistviewlayoutrow-height

提问by JohnIdol

I've got a listView displaying a few text records. I need to increase the height of rows (working on a touch screen so I need thicker rows) without increasing the font size.

我有一个 listView 显示一些文本记录。我需要增加行的高度(在触摸屏上工作,所以我需要更厚的行)而不增加字体大小。

This is probably pretty trivial but I have no clue and can't find much on google.

这可能是微不足道的,但我不知道,在谷歌上找不到太多。

Any help appreciated.

任何帮助表示赞赏。

回答by Andy

You can set the height of all ListViewItemsin a ListViewby using ItemContainerStyle:

您可以设置所有的高度ListViewItemsListView使用ItemContainerStyle

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

回答by Thomas Sandberg

Or you could use styles to set it for all listviews. Here scoped to within a window:

或者您可以使用样式为所有列表视图设置它。这里的范围是在一个窗口内:

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

回答by Henrik P. Hessel

In XAML

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>

In C# Codebehind

在 C# 代码隐藏中

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

Hope you getting the Idea.

希望你得到这个想法。