Pregunta

I tried to search Google, but i cannot formulate it clearly enough.

So idea is: if text cannot be displayed fully in a TextBlock, end of text should be removed and '...' should be added. For fixed size i can write something like this:

DetailsListView.Items.Add(string.Format("{0}.{1} - {2}...", i + 1, j + 1,  somestring.Remove(15)));

but for dynamic WPF layout that looks ugly.

What the best way to do it?


i'm trying this one, but it doesn't work

    <ListView Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Margin="5" Name="TaskListView">
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock TextTrimming="CharacterEllipsis" Text="{Binding}"></TextBlock>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

just a scrollbar appears

enter image description here

¿Fue útil?

Solución

No need to do it yourself, TextBlock can already do this for you:

<TextBlock TextTrimming="CharacterEllipsis"/>

It seems your text is in a list of some sort you need to set this on the TextBlock in a DataTemplate for each item in the list, e.g.:

<ListView ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock TextTrimming="CharacterEllipsis"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Of course if your TextBlock is in a pane that grows as it needs then there is nothing to trim - you can set a fixed size on your TextBlock, or your ListView and disable horizontal scrolling.

Otros consejos

<TextBlock 
   TextTrimming="WordEllipsis" TextWrapping="NoWrap"
   FontSize="14">
   One<LineBreak/>
   two two<LineBreak/>
   Three Three Three<LineBreak/>
   four four four four<LineBreak/>
   Five Five Five Five Five<LineBreak/>
   six six six six six six<LineBreak/>
   Seven Seven Seven Seven Seven Seven Seven
</TextBlock>

TextBlock.TextTrimming

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top