简体   繁体   中英

How to restrict length of cell in ListView?

I have such a list view在此处输入图片说明

And there is a .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.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. 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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