如何在 WPF ListView 中包装内容?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13056040/
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-13 05:55:59  来源:igfitidea点击:

How do I wrap content in a WPF ListView?

wpflistview

提问by Aaron C

I have a very simple WPF ListView that I'm using to list blocks of text. I'd like it to scroll vertically, but the text should wrap so that there is no horizontal scroll. All the examples I've seen are overly convoluted DataGridView nesting solutions. This seems like such a simple use-case, however. Here is my current code:

我有一个非常简单的 WPF ListView,用于列出文本块。我希望它垂直滚动,但文本应该换行,以便没有水平滚动。我见过的所有示例都是过于复杂的 DataGridView 嵌套解决方案。然而,这似乎是一个如此简单的用例。这是我当前的代码:

<ListView  
        Height="Auto"
        Width="Auto"
        Margin="0"
        Name="mLogListView" 
        FontWeight="Bold"
        FontSize="16"
        SelectionMode="Single"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        HorizontalContentAlignment="Stretch"/>

I've tried setting the ScrollViewer.HorizontalScrollBarVisibility and HorizontalContentAlignment properties but the text simply runs off the end of the control and doesn't wrap.

我已经尝试设置 ScrollViewer.Horizo​​ntalScrollBarVisibility 和 Horizo​​ntalContentAlignment 属性,但文本只是从控件的末尾运行并且不换行。

Each item is added to the ListView.Itemscollection and is a ListViewItemobject. The text is set to the item's Contentproperty.

每个项目都添加到ListView.Items集合中并且是一个ListViewItem对象。文本设置为项目的Content属性。

Here is the code responsible for adding text times to the list:

这是负责将文本时间添加到列表的代码:

ListViewItem item = new ListViewItem();            
item.Content = "Item text is set here, but refuses to wrap in list view!";
mLogListView.Items.Add(item);

Thank you.

谢谢你。

回答by Klaus78

This should be what you need

这应该是你需要的

<ListView Margin="12,23,309,191"
        Name="mLogListView" 
        FontWeight="Bold"
        FontSize="16"
        SelectionMode="Single"
        ScrollViewer.HorizontalScrollBarVisibility="Disabled"
        HorizontalContentAlignment="Stretch" >
<!-- here set the itemTemplate to a TextBlock that can wraps-->
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Path=.}" TextWrapping="Wrap"></TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

Note the syntax Text="{Binding Path=.}"that is equivalent to Text="{Binding}". This is called empty binding syntax.

请注意Text="{Binding Path=.}"等效于Text="{Binding}". 这称为空绑定语法

In this case Textis bound to the entire ListViewItem object. The empty binding syntax is useful when you want to bind to the entire object item instead of just to single property of the item.

在这种情况下Text绑定到整个 ListViewItem 对象。当您想要绑定到整个对象项目而不是仅绑定到项目的单个属性时,空绑定语法很有用。

This is convenient for the example because the source object (the ListViewItem) is of type string and you simply want to bind to the string itself.

这对于示例来说很方便,因为源对象(ListViewItem)是字符串类型,而您只想绑定到字符串本身。

For more information see msdnat section Specifying the Path to the Value

有关详细信息,请参阅指定值的路径部分的msdn

回答by peter70

I wanted to view images in a ListView, it displays by default only one column. To prevent this, you can insert the following code:

我想在 ListView 中查看图像,默认情况下它只显示一列。为了防止这种情况,您可以插入以下代码:

<ListView.ItemsPanel>
    <ItemsPanelTemplate>
        <WrapPanel Margin="0" />
    </ItemsPanelTemplate>
</ListView.ItemsPanel>

Now displays ListView, instead of one column, just one line :-( To prevent this, you can insert this line of code:

现在显示 ListView,而不是一列,只有一行 :-( 为了防止这种情况,您可以插入以下代码行:

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

So it could look something like this:

所以它看起来像这样:

<ListView ItemsSource="{Binding YourItemsSource...}"
            ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border ... and so on ...

I hope, it can help someone ;-)

我希望,它可以帮助某人;-)