简体   繁体   English

如何限制ListView中单元格的长度?

[英]How to restrict length of cell in ListView?

I have such a list view我有这样一个列表视图在此处输入图片说明

And there is a .xalm还有一个.xalm

...
<ListView
                x:Name="LVLog"
                ToolTip="Log of task(s) execution"
                Background="WhiteSmoke"
                HorizontalAlignment="Stretch"
                VerticalAlignment="Stretch"/>
...

And there is how I add items in LVLog还有我如何在LVLog添加项目

LVLog.Items.Add(message.Log);

As you can see if line a long enough it goes out of the borders and user need to scroll horizontaly in order to read log up to the end.如您所见,如果一行足够长,它就会超出边界,用户需要水平滚动才能将日志读到最后。

Question is: is there is a way to write line at the next line if it came to the borders?问题是:如果涉及到边界,是否有办法在下一行写一行?

You could use a TextBlock as an ItemTemplate and set TextTrimming on it.您可以使用TextBlock作为ItemTemplate并在其上设置TextTrimming So the text will be trimmed所以文本将被修剪

Long long lo..长长的..

and the full text you will see in tooltip:以及您将在工具提示中看到的全文:

<ListView ItemsSource="{Binding YourCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ...}" TextTrimming="CharacterEllipsis" ToolTip="{Binding ...}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

If you want to have more lines, then just set TextWrapping="Wrap" in the TextBlock :如果你想有更多的行,那么只需在TextBlock设置TextWrapping="Wrap"

<ListView ItemsSource="{Binding YourCollection}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ...}" TextWrapping="Wrap"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM