I have been doing most of my frontend in windowsforms but I thought it was time to switch to WPF after all it is already at version 4.0 so most of the little quirks should be gone by now.

First thing I did was to convert my dashboard over to it. I can use all the same controllers and model so I only need to worry about the views.

On my dashboard there is a ListBox which shows the user some messages of the system (not that anybody really reads them, but it looks cool).

The text for each item can be longer than the width of the ListBox. This is no problem since there is a horizontal scrollbar that can be seen. But I don’t like that.

And this is the XAML you use for this.

<ListBox Name ="lstMessages" />

And I filled the ListBox via this method. Which is an event coming from the ViewModel. Witch needs to be made threadsafe.

```vbnet Private Delegate Sub MessagesCallBack(ByVal Messages As System.Collections.Generic.Queue(Of String))

    Private Sub _Messages_MessageAdded(ByVal Messages As System.Collections.Generic.Queue(Of String)) Handles _Messages.MessageAdded
        If lstMessages.Dispatcher.CheckAccess Then
            lstMessages.ItemsSource = Messages.Reverse.ToArray()
        Else
            Dim d = New MessagesCallBack(AddressOf _Messages_MessageAdded)
            lstMessages.Dispatcher.Invoke(d, New Object() {Messages})
        End If
    End Sub```

So I’m using the Itemsource for this.

I would prefer some text trimming (the three dots) and a tooltip with the complete text.

In Windowsforms this would just be difficult to do. In WPF this is very easy.

You just need to add a Datatemplate like this.

xml <DataTemplate x:Key="itemTemplate"> <TextBlock TextTrimming="CharacterEllipsis" Text="{Binding}" ToolTip="{Binding}"></TextBlock> </DataTemplate> And then change your ListBox a little.

xml <ListBox Name ="lstMessages" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ItemTemplate="{StaticResource itemTemplate}"> And that is it. As you can see I removed the Horizontalscrollbar and added an ItemTemplate.

Or you can make it wrap instead. Just a little change in the XAML.

xml <DataTemplate x:Key="itemTemplate"> <TextBlock TextWrapping="Wrap" Text="{Binding}"></TextBlock> </DataTemplate> And then it looks like this. I guess you could then do without the tooltip so I removed that.

The hardest thing to find on the internets was the Text=”{Binding}”. I actually didn’t find it on the internets I just got lucky.