I'm having a problem aligning string contents in a listbox. Here's a distilled example that demonstrates the problem my app is having:
Basic XAML UI:
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="687" Loaded="Window_Loaded">
<Grid>
<ListBox x:Name="lstClients" HorizontalAlignment="Left" Height="220" Margin="28,22,0,0" VerticalAlignment="Top" Width="625"/>
</Grid>
</Window>
Code behind:
Class MainWindow
Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
Dim names() As String = {"Cook, Adam", "Renolds, Bridgette", "Smith, Carla", "Doe, Daniel",
"Redmond, Ebenezer", "Kelly, Francine"}
Dim ids() As String = {"123456", "654321", "112345", "274587", "128493", "937401"}
Dim dob() As String = {"1/2/80", "4/17/88", "8/31/72", "6/11/91", "10/27/81", "3/3/75"}
Dim phones() As String = {"1234567890", "2536772364", "1537779004", "7586712164", "8548778384", "0987654321"}
For i As Integer = 0 To 5
lstClients.Items.Add(FormatClientString(names(i), ids(i), dob(i), phones(i)))
Next
End Sub
Private Function FormatClientString(name As String, id As String, birthDate As String, phone As String)
Dim clientString As String
clientString = String.Format("{0, -52} {1, -17} {2, -20} {3}", name, id, birthDate, phone)
Return clientString
End Function
End Class
And here is what I am actually trying to accomplish (crappy paint editing but basically I just want the columns to align, regardless of the length of the client's name):
From what I read, String.Format should do what I want, but the output is never right. I also tried using String.PadRight for each string (names, ids, dob, and phones) but got the same behavior (misaligned columns).
Am I overlooking something simple?
EDIT: The control in my actual app is not a ListBox. It is an AutoCompleteBox from the WPF Toolkit. I used the ListBox above as an example because it was exhibiting the same behavior formatting the strings and was easier to make a simple example with.
If anyone knows how to add columns to the AutoCompleteBox's dropdown suggestions, that would work too as this is ultimately the goal.